— Latest Release: <html>NoJS</html>
test
use Array: all; double[.,.] matmul (double[.,.] a, double[.,.] b) { // transpose bt = { [i,j] -> b[j,i] }; // multiplication return { iv -> sum (a[iv[0]] * bt[iv[1]]) | iv < [shape(a)[0],shape(b)[1]] }; } int main() { // create matrices A = reshape ([6,8], tod (iota (48))); B = reshape ([8,2], tod (iota (16))); C = matmul (A, B); StdIO::print (C); return 0; }
int [+], int scan (int [+] a) { // TODO TC multiple return not supported yet a, m = { iv -> scan (a[iv ]) | iv < drop ([ -1] , shape (a)) }; m, s = scan (m); a = a ^+ m; return (a, s); }
The all pair N-body problem can be almost literally transliterated from its mathematical formulation into a valid SaC program:
k+1pi=kpi+kvidtk+1vi=kvi+kaidtk+1ai=n∑j≠imj(k+1pj−k+1pi)|k+1pj−k+1pi|3k+1pi=kpi+kvidtk+1vi=kvi+kaidtk+1ai=n∑j≠imj(k+1pj−k+1pi)∣∣∣k+1pj−k+1pi∣∣∣3
p = p + v * dt; v = v + a * dt; a = {[i] -> vsum ({[j] -> (i == j ? [0.0, 0.0, 0.0] : m[j] * (p[j] - p[i]) / (l2norm (p[j] - p[i]) ^ 3) }) };