Last active
December 20, 2015 15:59
-
-
Save SystemR/6158111 to your computer and use it in GitHub Desktop.
Some port of numpy functions to dart References:
http://attractivechaos.wordpress.com/2012/10/26/dart-revisiting-matrix-multiplication/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
List square(a, factor) { | |
inner_loop(n, ai, bi) { | |
List xi = new List<double>(n); | |
for (int j = 0; j < n; ++j) { | |
double sum = 0.0; | |
xi[j] = pow(ai[j], factor); | |
} | |
return xi; | |
} | |
int m = a.length, n = a[0].length; | |
List x = new List(m); | |
for (int i = 0; i < m; ++i) { | |
x[i] = inner_loop(n, a[i], factor); | |
} | |
return x; | |
} | |
List transpose(a) { | |
int m = a.length, n = a[0].length; // m rows and n cols | |
List b = new List(n); | |
for (int j = 0; j < n; ++j) b[j] = new List<double>(m); | |
for (int i = 0; i < m; ++i) { | |
for (int j = 0; j < n; ++j) { | |
b[j][i] = a[i][j]; | |
} | |
} | |
return b; | |
} | |
List tile(a, m, n) { | |
n = n * a.length; | |
List b = new List(m); | |
int aLength = a.length; | |
for (int i = 0; i < m; i++) { | |
b[i] = new List<double>(n); | |
for (int j = 0; j < n; j++) { | |
b[i][j] = a[j % aLength]; | |
} | |
} | |
return b; | |
} | |
List multiply (a, b) { | |
inner_loop(t, n, ai, c) { | |
List xi = new List<double>(t); | |
for (int j = 0; j < t; ++j) { | |
double sum = 0.0; | |
for (int k = 0; k < n; ++k) sum += ai[k] * c[j][k]; | |
xi[j] = sum; | |
} | |
return xi; | |
} | |
int m = a.length, n = a[0].length, s = b.length, t = b[0].length; | |
if (n != s) return null; | |
List x = new List(m), c = transpose(b); | |
for (int i = 0; i < m; ++i) { | |
x[i] = inner_loop(t, n, a[i], c); | |
} | |
return x; | |
} | |
List subtract(a, b) { | |
inner_loop(n, ai, bi) { | |
List xi = new List<double>(n); | |
for (int j = 0; j < n; ++j) { | |
double sum = 0.0; | |
xi[j] = ai[j] - bi[j]; | |
} | |
return xi; | |
} | |
int m = a.length, n = a[0].length, s = b.length, t = b[0].length; | |
if (n != t) return null; | |
List x = new List(m); | |
for (int i = 0; i < m; ++i) { | |
x[i] = inner_loop(n, a[i], b[i]); | |
} | |
return x; | |
} | |
List generate(int n) { | |
var a = new List(n); | |
double t = 1.0 / n / n; | |
for (int i = 0; i < n; ++i) { | |
a[i] = new List<double>(n); | |
for (int j = 0; j < n; ++j) | |
a[i][j] = t * (i - j) * (i + j); | |
} | |
return a; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment