#!/usr/bin/env python
# (C) 2000 Huaiyu Zhu <hzhu@users.sourceforge.net>. Licence: GPL
# $Id: Tensor.py,v 1.4 2000/07/26 20:58:27 hzhu Exp $
"""
Tensor class that complements Matrix class
"""
from MatPy import MatrixType
from MatPy.Matrix import to_array
import MLab
class Tensor(MatrixType):
"class Tensor : rudimentary wrapper around NumPy arrays"
def __init__(self, data):
if type(data) == type(MLab.array([])) or type(data) == type([]):
self.data = MLab.array(data)
elif isinstance(data, Tensor):
import copy
self.data = copy.copy(data.data)
else:
try:
assert data == data + 0
except:
print "data is %s" % data
raise
self.data = MLab.array([data])
self.shape = self.data.shape
self.typecode = self.data.typecode()
def __getitem__(self, i): return Tensor(self.data[i])
def __setitem__(self, i, item): self.data[i] = to_array(item)
def __mul__(self, other):
return self.dot(other)
def zeros(a, typecode='d'):
"zeros(a, typecode) : return a zero tensor"
return Tensor(MLab.zeros(a, typecode))
|