matrix X porting from old python




import bpy 

a=((1,  1,  1,   1),							# matrix A 
     (2,  4,  8,  16),
     (3,  9, 27,  81),
     (4, 16, 64, 256))
 
b=((  4  , -3  ,  4/3.,  -1/4. ),				# matrix B 
     (-13/3., 19/4., -7/3.,  11/24.),
     (  3/2., -2.  ,  7/6.,  -1/4. ),
     ( -1/6.,  1/4., -1/6.,   1/24.))
 
print ('a = ',a)
print () 

print ('b = ',b)
print () 
 
def MatrixMul( mtx_a, mtx_b):

    tpos_b = zip( *mtx_b)						#  Row = Col  transpose
#    print ('list zipped tpos_b= ',list(tpos_b))
    
    print ()
    rtn = [[ sum( ea*eb for ea,eb in zip(a,b)) for b in tpos_b] for a in mtx_a]
    
    return rtn
  
v = MatrixMul( a, b )
 
print ('v = ',v)
print ()


trying to port this old python script to the latest one

it does only the first item part of the product
all the other terms = []

can someone help to make this works again in latest python

thanks

import bpy
from mathutils import Matrix

a = Matrix((
        (1, 1, 1, 1),
        (2, 4, 8, 16),
        (3, 9, 27, 81),
        (4, 16, 64, 256),
    ))
    
b = Matrix((
        (4, -3, 4/3, -1/4),
        (-13/3, 19/4, -7/3, 11/24),
        (3/2, -2, 7/6, -1/4),
        (-1/6, 1/4, -1/6, 1/24),
    ))
    
v = a * b

print("v = %r" % v)

If the result isn’t what you expect, see:

i’m not using any of the internal matrix from blender

this is from roseta site and use only pure python

but he is using tuples and
the mult is giving results in only the first list item when run in blender
so I guess there was some changes since this was written

and wondering if possible to make it work again in latest python

thanks

This will be horribly slow: it really is better to use either Blender mathutils or NumPy.

But if you want to make it work just as an exercise, the problem is the transposition. zip(*mtx_b) returns an ‘disposable’ iterable view that you only get to use once, but you need it for each row. To create a real, reusable object, wrap it in list() or tuple():
tpos_b = tuple(zip( *mtx_b))

Best wishes,
Matthew