Startup
This commit is contained in:
36
script/__Lib/diffcalc/numjy/__init__.py
Executable file
36
script/__Lib/diffcalc/numjy/__init__.py
Executable file
@@ -0,0 +1,36 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
try:
|
||||
import Jama
|
||||
from numjy import linalg
|
||||
from numjy.jama_matrix_wrapper import matrix
|
||||
JAMA = True
|
||||
except ImportError:
|
||||
JAMA = False
|
||||
|
||||
|
||||
def hstack(list_of_column_matrices):
|
||||
if not Jama:
|
||||
raise Exception('Jama not available, use numpy directly')
|
||||
ncol = len(list_of_column_matrices)
|
||||
nrow = list_of_column_matrices[0].shape[0]
|
||||
m = Jama.Matrix(nrow, ncol)
|
||||
for c, column_matrix in enumerate(list_of_column_matrices):
|
||||
m.setMatrix(0, nrow - 1, c, c, column_matrix.m)
|
||||
return matrix(m)
|
||||
120
script/__Lib/diffcalc/numjy/jama_matrix_wrapper.py
Executable file
120
script/__Lib/diffcalc/numjy/jama_matrix_wrapper.py
Executable file
@@ -0,0 +1,120 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
import Jama
|
||||
|
||||
|
||||
class matrix(object):
|
||||
|
||||
def __init__(self, a):
|
||||
if isinstance(a, Jama.Matrix):
|
||||
self.m = a
|
||||
elif isinstance(a, basestring):
|
||||
l = []
|
||||
for row in a.strip().split(';'):
|
||||
l.append([float(element)
|
||||
for element in row.replace(',', ' ').split()])
|
||||
self.m = Jama.Matrix(l)
|
||||
elif isinstance(a, matrix):
|
||||
self.m = Jama.Matrix(a.tolist())
|
||||
elif isinstance(a, (list, tuple)):
|
||||
if isinstance(a[0], (list, tuple)):
|
||||
# a is a list of lists (not rigorous test!)
|
||||
self.m = Jama.Matrix(a)
|
||||
else:
|
||||
# a is a row vector
|
||||
self.m = Jama.Matrix([a])
|
||||
else:
|
||||
# give it a go
|
||||
self.m = Jama.Matrix(a)
|
||||
|
||||
def __eq__(self, other):
|
||||
nrow, ncol = self.shape
|
||||
b = matrix(Jama.Matrix(nrow, ncol))
|
||||
for i in range(nrow):
|
||||
for j in range(ncol):
|
||||
b[i, j] = self[i, j] == other[i, j]
|
||||
return b
|
||||
|
||||
@property
|
||||
def shape(self):
|
||||
return self.m.getRowDimension(), self.m.getColumnDimension()
|
||||
|
||||
def __len__(self):
|
||||
return self.m.getRowDimension()
|
||||
|
||||
def all(self): # @ReservedAssignment
|
||||
for row in self.m.array:
|
||||
if not all(row):
|
||||
return False
|
||||
return True
|
||||
|
||||
def tolist(self):
|
||||
l = []
|
||||
nrow, ncol = self.shape
|
||||
for i in range(nrow):
|
||||
row = []
|
||||
for j in range(ncol):
|
||||
row.append(self[i, j])
|
||||
l.append(row)
|
||||
return l
|
||||
|
||||
def sum(self): # @ReservedAssignment
|
||||
return sum(sum(row) for row in self.m.array)
|
||||
|
||||
@property
|
||||
def I(self):
|
||||
return matrix(self.m.inverse())
|
||||
|
||||
@property
|
||||
def T(self):
|
||||
return matrix(self.m.transpose())
|
||||
|
||||
def _scaler(self, scaler):
|
||||
return Jama.Matrix(self.shape[0], self.shape[1], scaler)
|
||||
|
||||
def __add__(self, other):
|
||||
v = other.m if isinstance(other, matrix) else self._scaler(other)
|
||||
return matrix(self.m.plus(v))
|
||||
|
||||
def __sub__(self, other):
|
||||
v = other.m if isinstance(other, matrix) else self._scaler(other)
|
||||
return matrix(self.m.minus(v))
|
||||
|
||||
def __mul__(self, other):
|
||||
return matrix(self.m.times(other.m if isinstance(other, matrix) else
|
||||
other))
|
||||
|
||||
def __div__(self, other):
|
||||
# dividend = other.I if isinstance(other, matrix) else 1. /float(other)
|
||||
return self.__mul__(1. / float(other))
|
||||
|
||||
def __getitem__(self, key):
|
||||
i, j = key
|
||||
return self.m.get(i, j)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
i, j = key
|
||||
self.m.set(i, j, value)
|
||||
|
||||
def __str__(self):
|
||||
insides = [' '.join([str(el) for el in row]) for row in self.tolist()]
|
||||
return '[[' + ']\n ['.join(insides) + ']]'
|
||||
|
||||
def __repr__(self):
|
||||
return 'matrix(' + '\n '.join(self.__str__().split('\n')) + ')'
|
||||
20
script/__Lib/diffcalc/numjy/linalg.py
Executable file
20
script/__Lib/diffcalc/numjy/linalg.py
Executable file
@@ -0,0 +1,20 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
def norm(mat):
|
||||
return mat.m.normF()
|
||||
Reference in New Issue
Block a user