next up previous contents
Next: Tests of various transpose Up: Getting started and examples Previous: Tests of matrix functions   Contents

Tests of tensor operations

The file test_tensor.py tests some tensor operations. Results are at 3.13


#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>.  Licence: GPL
# $Id: test_tensor.py,v 1.2 2000/06/22 23:56:15 hzhu Exp $
"""
Test Tensor module 
"""

from MatPy.Tensor import *
from MatPy import formats
formats.format = lambda x:"% -5.2g"%x

A = B = Tensor([[[1.,2],[3,4]],[[5,6],[7,8]]])
print A, A.shape
print A[0], A[0].shape
print A[0,1], A[0,1].shape
print A[0,1,1], A[0,1,1].shape

print "-"*40
A[0,0,0] = 9
print A, A.shape

print "-"*40
A = Tensor([[1,2],[3,4]])
print A, A.shape

print "-"*40
A = Tensor([[1,2,3,4]])
print A, A.shape

print "-"*40
A = Tensor([1,2,3,4])
print A, A.shape

print "-"*40
A = Tensor([1])
print A, A.shape

print "-"*40
A = Tensor(1)
print A, A.shape

print "-"*40
A = Tensor([])
print A, A.shape

print "-"*40
from MLab import rand
B[0,0,:] = rand(2)
print B, B.shape

print "========================================================"
print "Testing changing format"
a = MLab.arange(4)/3.

print formats.strF(a)
print formats.reprF(a),  "\n", "-"*40

a = MLab.array([a+1, a-1])
formats.formatF = lambda x:"% -7.4g"%x
print formats.strF(a)
print formats.reprF(a), "\n", "-"*40

a = MLab.array([a*4,a+10,a-1])
formats.formatF = lambda x:"% -5.2g"%x
print formats.strF(a)
print formats.reprF(a), "\n", "-"*40


Result obtained with
>>> from MatPy.tests import test_tensor


[[ 1      2    
   3      4     ]
 [ 5      6    
   7      8     ] ] (2, 2, 2)
[ 1      2    
  3      4     ] (2, 2)
 3      4     (2,)
 4     (1,)
----------------------------------------
[[ 9      2    
   3      4     ]
 [ 5      6    
   7      8     ] ] (2, 2, 2)
----------------------------------------
[ 1   2 
  3   4  ] (2, 2)
----------------------------------------
[ 1   2   3   4  ] (1, 4)
----------------------------------------
 1   2   3   4  (4,)
----------------------------------------
 1  (1,)
----------------------------------------
 1  (1,)
----------------------------------------
 (0,)
----------------------------------------
[[ 0.828  0.597
   3      4     ]
 [ 5      6    
   7      8     ] ] (2, 2, 2)
========================================================
Testing changing format
 0      0.333  0.667  1    
[  0    ,  0.333,  0.667,  1     ] 
----------------------------------------
[ 1       1.333   1.667   2     
 -1      -0.6667 -0.3333  0      ]
[[  1     ,  1.333 ,  1.667 ,  2      ],
 [ -1     , -0.6667, -0.3333,  0      ]] 
----------------------------------------
[[ 4     5.3   6.7   8   
  -4    -2.7  -1.3   0    ]
 [ 11    11    12    12  
   9     9.3   9.7   10   ]
 [ 0     0.33  0.67  1   
  -2    -1.7  -1.3  -1    ] ]
[[[  4   ,  5.3 ,  6.7 ,  8    ],
  [ -4   , -2.7 , -1.3 ,  0    ]],
 [[  11  ,  11  ,  12  ,  12   ],
  [  9   ,  9.3 ,  9.7 ,  10   ]],
 [[  0   ,  0.33,  0.67,  1    ],
  [ -2   , -1.7 , -1.3 , -1    ]]] 
----------------------------------------



Huaiyu Zhu
2002-03-23