Skip to content

Instantly share code, notes, and snippets.

@fospathi
Created November 24, 2017 08:04
Show Gist options
  • Save fospathi/2523a102d2e9549262a0279c929d95a6 to your computer and use it in GitHub Desktop.
Save fospathi/2523a102d2e9549262a0279c929d95a6 to your computer and use it in GitHub Desktop.
/// Print the calculation which multiplies two 4x4 matrices.
///
/// The matrices store their elements in a flat array in column order.
void main() {
StringBuffer buffer = new StringBuffer();
String l = "m1";
String r = "m2";
for (int col in new Iterable.generate(4)) {
for (int row in new Iterable.generate(4)) {
buffer.writeAll([
"$l[${0 + row}]*$r[${col * 4 + 0}]",
"$l[${4 + row}]*$r[${col * 4 + 1}]",
"$l[${8 + row}]*$r[${col * 4 + 2}]",
"$l[${12 + row}]*$r[${col * 4 + 3}]",
], " + ");
buffer.write(",\n");
}
}
print(buffer.toString());
}
/*
m1[0]*m2[0] + m1[4]*m2[1] + m1[8]*m2[2] + m1[12]*m2[3],
m1[1]*m2[0] + m1[5]*m2[1] + m1[9]*m2[2] + m1[13]*m2[3],
m1[2]*m2[0] + m1[6]*m2[1] + m1[10]*m2[2] + m1[14]*m2[3],
m1[3]*m2[0] + m1[7]*m2[1] + m1[11]*m2[2] + m1[15]*m2[3],
m1[0]*m2[4] + m1[4]*m2[5] + m1[8]*m2[6] + m1[12]*m2[7],
m1[1]*m2[4] + m1[5]*m2[5] + m1[9]*m2[6] + m1[13]*m2[7],
m1[2]*m2[4] + m1[6]*m2[5] + m1[10]*m2[6] + m1[14]*m2[7],
m1[3]*m2[4] + m1[7]*m2[5] + m1[11]*m2[6] + m1[15]*m2[7],
m1[0]*m2[8] + m1[4]*m2[9] + m1[8]*m2[10] + m1[12]*m2[11],
m1[1]*m2[8] + m1[5]*m2[9] + m1[9]*m2[10] + m1[13]*m2[11],
m1[2]*m2[8] + m1[6]*m2[9] + m1[10]*m2[10] + m1[14]*m2[11],
m1[3]*m2[8] + m1[7]*m2[9] + m1[11]*m2[10] + m1[15]*m2[11],
m1[0]*m2[12] + m1[4]*m2[13] + m1[8]*m2[14] + m1[12]*m2[15],
m1[1]*m2[12] + m1[5]*m2[13] + m1[9]*m2[14] + m1[13]*m2[15],
m1[2]*m2[12] + m1[6]*m2[13] + m1[10]*m2[14] + m1[14]*m2[15],
m1[3]*m2[12] + m1[7]*m2[13] + m1[11]*m2[14] + m1[15]*m2[15],
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment