-
Notifications
You must be signed in to change notification settings - Fork 98
chore: add matrix multiplication as e2e test #311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
37011f3
fc021bc
675ab2a
943c397
e166ede
a1d9050
8a914f1
2d97c87
70e7b99
8736801
0f39745
1ff7ee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| type Vec = [Int]; | ||
|
|
||
| actor { | ||
| public func dot_product(a:Vec, b:Vec) : async Int { | ||
| assert (a.len() == b.len()); | ||
| var res: Int = 0; | ||
| let len = a.len(); | ||
| var i = 0; | ||
| while (i < len) { | ||
| res := res + a[i]*b[i]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should add support for matrix operations/SIMD 😂
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Machine Learning!!!
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! |
||
| i += 1; | ||
| }; | ||
| res | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import A "mo:stdlib/array.mo"; | ||
| import D "canister:dot_product"; | ||
|
|
||
| type Matrix = [[Int]]; | ||
|
|
||
| actor { | ||
| public func transpose(m: Matrix) : async Matrix { | ||
| assert (m.len() > 0); | ||
| let n_row = m.len(); | ||
| let n_col = m[0].len(); | ||
| A.tabulate<[Int]>( | ||
| n_col, | ||
| func (j:Nat) : [Int] { | ||
| A.tabulate<Int>(n_row, func (i:Nat) : Int = (m[i][j])); | ||
| }); | ||
| }; | ||
| public func multiply(a: Matrix, b: Matrix) : async Matrix { | ||
| assert (a.len() > 0 and b.len() > 0); | ||
| assert (a[0].len() == b.len()); | ||
| let n = a.len(); | ||
| let k = b[0].len(); | ||
| let bt = await transpose(b); | ||
| let res : [[var Int]] = A.tabulate<[var Int]>(n, func (_:Nat):[var Int] = A.init<Int>(k, 0)); | ||
| var i = 0; | ||
| while (i < n) { | ||
| var j = 0; | ||
| while (j < k) { | ||
| res[i][j] := await D.dot_product(a[i], bt[j]); | ||
| j += 1; | ||
| }; | ||
| i += 1; | ||
| }; | ||
| A.tabulate<[Int]>( | ||
| n, | ||
| func (i:Nat) : [Int] { A.freeze<Int>(res[i]) }); | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| dfx config --new_canister dot_product | ||
| dfx config canisters/dot_product/main dot_product.mo | ||
| dfx config canisters/hello/main matrix.mo |
Uh oh!
There was an error while loading. Please reload this page.