Skip to content

Instantly share code, notes, and snippets.

@apetrone
Created July 5, 2013 20:25
Show Gist options
  • Save apetrone/5937013 to your computer and use it in GitHub Desktop.
Save apetrone/5937013 to your computer and use it in GitHub Desktop.
Matrix transpose to C code
# This was created as a matrix utility to create C code from matrix notations
colMajor = {
11 : 0, 12 : 4, 13 : 8, 14 : 12,
21 : 1, 22 : 5, 23 : 9, 24 : 13,
31 : 2, 32 : 6, 33 : 10, 34 : 14,
41 : 3, 42 : 7, 43 : 11, 44 : 15
}
rowMajor = {
11 : 0, 12 : 1, 13 : 2, 14 : 3,
21 : 4, 22 : 5, 23 : 6, 24 : 7,
31 : 8, 32 : 9, 33 : 10, 34 : 11,
41 : 12, 42 : 13, 43 : 14, 44 : 15
}
#print ( colMajor[11] )
inv = """
11 = (22 * 33 * 44 + 23 * 34 * 42 + 24 * 32 * 43) - (22 * 34 * 43 - 23 * 32 * 44 - 24 * 33 * 42);
12 = (12 * 34 * 43 + 13 * 32 * 44 + 14 * 33 * 42) - (12 * 33 * 44 - 13 * 34 * 32 - 14 * 32 * 43);
13 = (12 * 23 * 44 + 13 * 24 * 42 + 14 * 22 * 43) - (12 * 24 * 43 - 13 * 22 * 44 - 14 * 23 * 42);
14 = (12 * 24 * 43 + 13 * 22 * 34 + 14 * 23 * 32) - (12 * 23 * 34 - 13 * 24 * 32 - 14 * 22 * 33);
21 = (21 * 34 * 43 + 23 * 31 * 44 + 24 * 33 * 41) - (21 * 33 * 44 - 23 * 34 * 41 - 24 * 31 * 43);
22 = (11 * 33 * 44 + 13 * 34 * 41 + 14 * 31 * 43) - (11 * 34 * 43 - 13 * 31 * 44 - 14 * 33 * 41);
23 = (11 * 24 * 43 + 13 * 21 * 44 + 14 * 23 * 41) - (11 * 23 * 43 - 13 * 24 * 41 - 14 * 21 * 43);
24 = (11 * 23 * 34 + 13 * 24 * 31 + 14 * 21 * 33) - (11 * 24 * 33 - 13 * 21 * 34 - 14 * 23 * 31);
31 = (21 * 32 * 44 + 22 * 34 * 41 + 24 * 31 * 42) - (21 * 34 * 42 - 22 * 31 * 44 - 24 * 32 * 41);
32 = (11 * 34 * 42 + 12 * 31 * 44 + 14 * 32 * 41) - (11 * 32 * 44 - 12 * 34 * 41 - 14 * 31 * 42);
33 = (11 * 22 * 44 + 12 * 24 * 41 + 14 * 21 * 42) - (11 * 24 * 42 - 12 * 21 * 44 - 14 * 22 * 41);
34 = (11 * 24 * 32 + 12 * 21 * 34 + 14 * 22 * 31) - (11 * 22 * 34 - 12 * 24 * 31 - 14 * 21 * 32);
41 = (21 * 33 * 42 + 22 * 31 * 43 + 23 * 32 * 41) - (21 * 32 * 43 - 22 * 33 * 41 - 23 * 31 * 42);
42 = (11 * 32 * 43 + 12 * 33 * 41 + 13 * 31 * 42) - (11 * 33 * 42 - 12 * 31 * 43 - 13 * 32 * 41);
43 = (11 * 23 * 42 + 12 * 21 * 43 + 13 * 22 * 41) - (11 * 22 * 43 - 12 * 23 * 41 - 13 * 21 * 42);
44 = (11 * 22 * 33 + 12 * 23 * 31 + 13 * 21 * 32) - (11 * 23 * 32 - 12 * 21 * 33 - 13 * 22 * 31);
"""
inv2 = """
11 = 23 * 34 * 42 - 24 * 33 * 42 + 24 * 32 * 43 - 22 * 34 * 43 - 23 * 32 * 44 + 22 * 33 * 44;
12 = 14 * 33 * 42 - 13 * 34 * 42 - 14 * 32 * 43 + 12 * 34 * 43 + 13 * 32 * 44 - 12 * 33 * 44;
13 = 13 * 24 * 42 - 14 * 23 * 42 + 14 * 22 * 43 - 12 * 24 * 43 - 13 * 22 * 44 + 12 * 23 * 44;
14 = 14 * 23 * 32 - 13 * 24 * 32 - 14 * 22 * 33 + 12 * 24 * 33 + 13 * 22 * 34 - 12 * 23 * 34;
21 = 24 * 33 * 41 - 23 * 34 * 41 - 24 * 31 * 43 + 21 * 34 * 43 + 23 * 31 * 44 - 21 * 33 * 44;
22 = 13 * 34 * 41 - 14 * 33 * 41 + 14 * 31 * 43 - 11 * 34 * 43 - 13 * 31 * 44 + 11 * 33 * 44;
23 = 14 * 23 * 41 - 13 * 24 * 41 - 14 * 21 * 43 + 11 * 24 * 43 + 13 * 21 * 44 - 11 * 23 * 44;
24 = 13 * 24 * 31 - 14 * 23 * 31 + 14 * 21 * 33 - 11 * 24 * 33 - 13 * 21 * 34 + 11 * 23 * 34;
31 = 22 * 34 * 41 - 24 * 32 * 41 + 24 * 31 * 42 - 21 * 34 * 42 - 22 * 31 * 44 + 21 * 32 * 44;
32 = 14 * 32 * 41 - 12 * 34 * 41 - 14 * 31 * 42 + 11 * 34 * 42 + 12 * 31 * 44 - 11 * 32 * 44;
33 = 12 * 24 * 41 - 14 * 22 * 41 + 14 * 21 * 42 - 11 * 24 * 42 - 12 * 21 * 44 + 11 * 22 * 44;
34 = 14 * 22 * 31 - 12 * 24 * 31 - 14 * 21 * 32 + 11 * 24 * 32 + 12 * 21 * 34 - 11 * 22 * 34;
41 = 23 * 32 * 41 - 22 * 33 * 41 - 23 * 31 * 42 + 21 * 33 * 42 + 22 * 31 * 43 - 21 * 32 * 43;
42 = 12 * 33 * 41 - 13 * 32 * 41 + 13 * 31 * 42 - 11 * 33 * 42 - 12 * 31 * 43 + 11 * 32 * 43;
43 = 13 * 22 * 41 - 12 * 23 * 41 - 13 * 21 * 42 + 11 * 23 * 42 + 12 * 21 * 43 - 11 * 22 * 43;
44 = 12 * 23 * 31 - 13 * 22 * 31 + 13 * 21 * 32 - 11 * 23 * 32 - 12 * 21 * 33 + 11 * 22 * 33;
"""
mult = """
"""
determinant = """
(14 * 23 * 32 * 41-13 * 24 * 32 * 41-14 * 22 * 33 * 41 + 12 * 24 * 33 * 41+
13 * 22 * 34 * 41-12 * 23 * 34 * 41-14 * 23 * 31 * 42 + 13 * 24 * 31 * 42+
14 * 21 * 33 * 42-11 * 24 * 33 * 42-13 * 21 * 34 * 42 + 11 * 23 * 34 * 42+
14 * 22 * 31 * 43-12 * 24 * 31 * 43-14 * 21 * 32 * 43 + 11 * 24 * 32 * 43+
12 * 21 * 34 * 43-11 * 22 * 34 * 43-13 * 22 * 31 * 44 + 12 * 23 * 31 * 44+
13 * 21 * 32 * 44-11 * 23 * 32 * 44-12 * 21 * 33 * 44 + 11 * 22 * 33 * 44);
"""
src = determinant
import re;
matches = re.findall( '\d\d *', src, re.MULTILINE )
activeMapping = rowMajor
#print( matches )
def LookupValue( value ):
return activeMapping[ int(value) ]
keylist = activeMapping.items()
sortedkeys = sorted( activeMapping.keys(), key=lambda x: activeMapping[x])
#print( sortedkeys )
out = src
for m in sortedkeys:
# print( str(m) )
out = out.replace( str(m), 'out.m[' + str(LookupValue(m)) + '] ' )
print( out )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment