def stdForm(a, b): def invert(L): # Inverts lower triangular matrix L n = len(L) for j in range(n - 1): L[j, j] = 1.0 / L[j, j] for i in range(j + 1, n): L[i, j] = -dot(L[i, j:i], L[j:i, j]) / L[i, i] L[n - 1, n - 1] = 1.0 / L[n - 1, n - 1] n = len(a) L = choleski(b) invert(L) h = matrixmultiply(b, matrixmultiply(a, transpose(L))) return h, transpose(L)
def triangleQuad(f, xc, yc): alpha = array([[1.0/3, 1.0/3.0, 1.0/3.0], \ [0.2, 0.2, 0.6], \ [0.6, 0.2, 0.2], \ [0.2, 0.6, 0.2]]) W = array([-27.0 / 48.0, 25.0 / 48.0, 25.0 / 48.0, 25.0 / 48.0]) x = matrixmultiply(alpha, xc) y = matrixmultiply(alpha, yc) A = (xc[1]*yc[2] - xc[2]*yc[1] \ - xc[0]*yc[2] + xc[2]*yc[0] \ + xc[0]*yc[1] - xc[1]*yc[0])/2.0 sum = 0.0 for i in range(4): sum = sum + W[i] * f(x[i], y[i]) return A * sum
def triangleQuad(f,xc,yc): alpha = array([[1.0/3, 1.0/3.0, 1.0/3.0], \ [0.2, 0.2, 0.6], \ [0.6, 0.2, 0.2], \ [0.2, 0.6, 0.2]]) W = array([-27.0/48.0 ,25.0/48.0, 25.0/48.0, 25.0/48.0]) x = matrixmultiply(alpha,xc) y = matrixmultiply(alpha,yc) A = (xc[1]*yc[2] - xc[2]*yc[1] \ - xc[0]*yc[2] + xc[2]*yc[0] \ + xc[0]*yc[1] - xc[1]*yc[0])/2.0 sum = 0.0 for i in range(4): sum = sum + W[i] * f(x[i],y[i]) return A*sum
def gen_rotations(theta1, theta2, theta3): global rot_matrix theta1, theta2, theta3 = radians(theta1), radians(theta2), radians(theta3) rotate_x = array(sequence=(1, 0, 0, 0, cos(theta1), -sin(theta1), 0, sin(theta1), cos(theta1)),shape=(3,3)) rotate_y = array(sequence=(cos(theta2),0, -sin(theta2), 0, 1, 0, sin(theta2),0, cos(theta2)),shape=(3,3)) rotate_z = array(sequence=(cos(theta3), -sin(theta3), 0, sin(theta3), cos(theta3), 0, 0, 0, 1,),shape=(3,3)) rot_matrix = matrixmultiply(matrixmultiply(rotate_z,rotate_y),rotate_x)
def _gaussian(self, mean, cvm, x): m = len(mean) assert cvm.shape == (m, m), \ 'bad sized covariance matrix, %s' % str(cvm.shape) try: det = numarray.linear_algebra.determinant(cvm) inv = numarray.linear_algebra.inverse(cvm) a = det ** -0.5 * (2 * numarray.pi) ** (-m / 2.0) dx = x - mean b = -0.5 * numarray.matrixmultiply( \ numarray.matrixmultiply(dx, inv), dx) return a * numarray.exp(b) except OverflowError: # happens when the exponent is negative infinity - i.e. b = 0 # i.e. the inverse of cvm is huge (cvm is almost zero) return 0
def cluster(self, tokens, assign_clusters=False, trace=False): assert chktype(1, tokens, [Token]) assert chktype(2, assign_clusters, bool) assert chktype(3, trace, bool) assert len(tokens) > 0 vectors = map(lambda tk: tk['FEATURES'], tokens) # normalise the vectors if self._should_normalise: vectors = map(self._normalise, vectors) # use SVD to reduce the dimensionality if self._svd_dimensions and self._svd_dimensions < len(vectors[0]): [u, d, vt] = numarray.linear_algebra.singular_value_decomposition( numarray.transpose(numarray.array(vectors))) S = d[:self._svd_dimensions] * \ numarray.identity(self._svd_dimensions, numarray.Float64) T = u[:,:self._svd_dimensions] Dt = vt[:self._svd_dimensions,:] vectors = numarray.transpose(numarray.matrixmultiply(S, Dt)) self._Tt = numarray.transpose(T) # call abstract method to cluster the vectors self.cluster_vectorspace(vectors, trace) # assign the tokens to clusters if assign_clusters: for token in tokens: self.classify(token)
def getWeights(sample): # Evaluate PDFs for this sample. pds = numarray.array([ f(sample) for f in pdfs ]) # Compute an array of the weights. denominator = numarray.dot(nums, pds) if denominator != 0: return numarray.matrixmultiply(V, pds) / denominator else: return numarray.zeros((num_categories, ), "Float32")
def computeP(a): n = len(a) p = identity(n)*1.0 for k in range(n-2): u = a[k+1:n,k] h = dot(u,u)/2.0 v = matrixmultiply(p[1:n,k+1:n],u)/h p[1:n,k+1:n] = p[1:n,k+1:n] - outerproduct(v,u) return p
def likelihood(self, labelled_token): assert chktype(1, labelled_token, Token) vector = labelled_token['FEATURES'] #assert chktype('features', vector, numarray.array([]), SparseArray) if self._should_normalise: vector = self._normalise(vector) if self._Tt != None: vector = numarray.matrixmultiply(self._Tt, vector) return self.likelihood_vectorspace(vector, labelled_token['CLUSTER'])
def computeP(a): n = len(a) p = identity(n) * 1.0 for k in range(n - 2): u = a[k + 1:n, k] h = dot(u, u) / 2.0 v = matrixmultiply(p[1:n, k + 1:n], u) / h p[1:n, k + 1:n] = p[1:n, k + 1:n] - outerproduct(v, u) return p
def classify(self, token): assert chktype(1, token, Token) vector = token['FEATURES'] #assert chktype('features', vector, numarray.array([]), SparseArray) if self._should_normalise: vector = self._normalise(vector) if self._Tt != None: vector = numarray.matrixmultiply(self._Tt, vector) cluster = self.classify_vectorspace(vector) token['CLUSTER'] = self.cluster_name(cluster)
def vector(self, token): """ Returns the vector after normalisation and dimensionality reduction for the given token's FEATURES. """ assert chktype(1, token, Token) vector = token['FEATURES'] #assert chktype('features', vector, numarray.array([]), SparseArray) if self._should_normalise: vector = self._normalise(vector) if self._Tt != None: vector = numarray.matrixmultiply(self._Tt, vector) return vector
def householder(a): n = len(a) for k in range(n-2): u = a[k+1:n,k] uMag = sqrt(dot(u,u)) if u[0] < 0.0: uMag = -uMag u[0] = u[0] + uMag h = dot(u,u)/2.0 v = matrixmultiply(a[k+1:n,k+1:n],u)/h g = dot(u,v)/(2.0*h) v = v - g*u a[k+1:n,k+1:n] = a[k+1:n,k+1:n] - outerproduct(v,u) \ - outerproduct(u,v) a[k,k+1] = -uMag return diagonal(a),diagonal(a,1)
def householder(a): n = len(a) for k in range(n - 2): u = a[k + 1:n, k] uMag = sqrt(dot(u, u)) if u[0] < 0.0: uMag = -uMag u[0] = u[0] + uMag h = dot(u, u) / 2.0 v = matrixmultiply(a[k + 1:n, k + 1:n], u) / h g = dot(u, v) / (2.0 * h) v = v - g * u a[k+1:n,k+1:n] = a[k+1:n,k+1:n] - outerproduct(v,u) \ - outerproduct(u,v) a[k, k + 1] = -uMag return diagonal(a), diagonal(a, 1)
def rotate(input, angle, axes=(-1, -2), reshape=True, output_type=None, output=None, order=3, mode='constant', cval=0.0, prefilter=True): """Rotate an array. The array is rotated in the plane defined by the two axes given by the axes parameter using spline interpolation of the requested order. The angle is given in degrees. Points outside the boundaries of the input are filled according to the given mode. If reshape is true, the output shape is adapted so that the input array is contained completely in the output. The parameter prefilter determines if the input is pre- filtered before interpolation, if False it is assumed that the input is already filtered. """ input = numarray.asarray(input) axes = list(axes) rank = input.rank if axes[0] < 0: axes[0] += rank if axes[1] < 0: axes[1] += rank if axes[0] < 0 or axes[1] < 0 or axes[0] > rank or axes[1] > rank: raise RuntimeError, 'invalid rotation plane specified' if axes[0] > axes[1]: axes = axes[1], axes[0] angle = numarray.pi / 180 * angle m11 = math.cos(angle) m12 = math.sin(angle) m21 = -math.sin(angle) m22 = math.cos(angle) matrix = numarray.array([[m11, m12], [m21, m22]], type=numarray.Float64) iy = input.shape[axes[0]] ix = input.shape[axes[1]] if reshape: mtrx = numarray.array([[m11, -m21], [-m12, m22]], type=numarray.Float64) minc = [0, 0] maxc = [0, 0] coor = numarray.matrixmultiply(mtrx, [0, ix]) minc, maxc = _minmax(coor, minc, maxc) coor = numarray.matrixmultiply(mtrx, [iy, 0]) minc, maxc = _minmax(coor, minc, maxc) coor = numarray.matrixmultiply(mtrx, [iy, ix]) minc, maxc = _minmax(coor, minc, maxc) oy = int(maxc[0] - minc[0] + 0.5) ox = int(maxc[1] - minc[1] + 0.5) else: oy = input.shape[axes[0]] ox = input.shape[axes[1]] offset = numarray.zeros((2, ), type=numarray.Float64) offset[0] = float(oy) / 2.0 - 0.5 offset[1] = float(ox) / 2.0 - 0.5 offset = numarray.matrixmultiply(matrix, offset) tmp = numarray.zeros((2, ), type=numarray.Float64) tmp[0] = float(iy) / 2.0 - 0.5 tmp[1] = float(ix) / 2.0 - 0.5 offset = tmp - offset output_shape = list(input.shape) output_shape[axes[0]] = oy output_shape[axes[1]] = ox output_shape = tuple(output_shape) output, return_value = _ni_support._get_output(output, input, output_type, shape=output_shape) if input.rank <= 2: affine_transform(input, matrix, offset, output_shape, None, output, order, mode, cval, prefilter) else: coordinates = [] size = input.nelements() size /= input.shape[axes[0]] size /= input.shape[axes[1]] for ii in range(input.rank): if ii not in axes: coordinates.append(0) else: coordinates.append(slice(None, None, None)) iter_axes = range(input.rank) iter_axes.reverse() iter_axes.remove(axes[0]) iter_axes.remove(axes[1]) os = (output_shape[axes[0]], output_shape[axes[1]]) for ii in range(size): ia = input[tuple(coordinates)] oa = output[tuple(coordinates)] affine_transform(ia, matrix, offset, os, None, oa, order, mode, cval, prefilter) for jj in iter_axes: if coordinates[jj] < input.shape[jj] - 1: coordinates[jj] += 1 break else: coordinates[jj] = 0 return return_value
## example9_14 from householder import * from eigenvals3 import * from inversePower3 import * from numarray import array,zeros,Float64,matrixmultiply N = 3 # Number of eigenvalues requested a = array([[ 11.0, 2.0, 3.0, 1.0, 4.0], \ [ 2.0, 9.0, 3.0, 5.0, 2.0], \ [ 3.0, 3.0, 15.0, 4.0, 3.0], \ [ 1.0, 5.0, 4.0, 12.0, 4.0], \ [ 4.0, 2.0, 3.0, 4.0, 17.0]]) xx = zeros((len(a),N),type=Float64) d,c = householder(a) # Tridiagonalize [A] p = computeP(a) # Compute transformation matrix lambdas = eigenvals3(d,c,N) # Compute eigenvalues for i in range(N): s = lambdas[i]*1.0000001 # Shift very close to eigenvalue lam,x = inversePower3(d,c,s) # Compute eigenvector [x] xx[:,i] = x # Place [x] in array [xx] xx = matrixmultiply(p,xx) # Recover eigenvectors of [A] print "Eigenvalues:\n",lambdas print "\nEigenvectors:\n",xx raw_input("Press return to exit")
def scale_point(point, factor): t_scale = array(sequence=(factor, 0, 0, 0, factor, 0, 0, 0, factor), shape=(3,3)) return matrixmultiply(t_scale, array(sequence=point, shape=(3,1))).tolist()
def transform(self, t): tmp = transpose(array(sequence=self.points, shape=(len(self.points), 3))) self.points = transpose(matrixmultiply(t, tmp)).tolist()
def transformPoints(self, vector): b = matrixmultiply(self.T, vector) return b
## example9_14 from householder import * from eigenvals3 import * from inversePower3 import * from numarray import array, zeros, Float64, matrixmultiply N = 3 # Number of eigenvalues requested a = array([[ 11.0, 2.0, 3.0, 1.0, 4.0], \ [ 2.0, 9.0, 3.0, 5.0, 2.0], \ [ 3.0, 3.0, 15.0, 4.0, 3.0], \ [ 1.0, 5.0, 4.0, 12.0, 4.0], \ [ 4.0, 2.0, 3.0, 4.0, 17.0]]) xx = zeros((len(a), N), type=Float64) d, c = householder(a) # Tridiagonalize [A] p = computeP(a) # Compute transformation matrix lambdas = eigenvals3(d, c, N) # Compute eigenvalues for i in range(N): s = lambdas[i] * 1.0000001 # Shift very close to eigenvalue lam, x = inversePower3(d, c, s) # Compute eigenvector [x] xx[:, i] = x # Place [x] in array [xx] xx = matrixmultiply(p, xx) # Recover eigenvectors of [A] print "Eigenvalues:\n", lambdas print "\nEigenvectors:\n", xx raw_input("Press return to exit")
#!/usr/bin/python ## example2_8 from numarray import array,matrixmultiply,transpose from choleski import * a = array([[ 1.44, -0.36, 5.52, 0.0], \ [-0.36, 10.33, -7.78, 0.0], \ [ 5.52, -7.78, 28.40, 9.0], \ [ 0.0, 0.0, 9.0, 61.0]]) L = choleski(a) print 'L =\n',L print '\nCheck: L*L_transpose =\n', \ matrixmultiply(L,transpose(L)) raw_input("\nPress return to exit")
## example9_4 from numarray import array, matrixmultiply, dot from math import sqrt s = array([[-30.0, 10.0, 20.0], \ [ 10.0, 40.0, -50.0], \ [ 20.0, -50.0, -10.0]]) v = array([1.0, 0.0, 0.0]) for i in range(100): vOld = v.copy() z = matrixmultiply(s, v) zMag = sqrt(dot(z, z)) v = z / zMag if dot(vOld, v) < 0.0: sign = -1.0 v = -v else: sign = 1.0 if sqrt(dot(vOld - v, vOld - v)) < 1.0e-6: break lam = sign * zMag print "Number of iterations =", i print "Eigenvalue =", lam raw_input("Press return to exit")
#!/usr/bin/python ## example2_8 from numarray import array, matrixmultiply, transpose from choleski import * a = array([[ 1.44, -0.36, 5.52, 0.0], \ [-0.36, 10.33, -7.78, 0.0], \ [ 5.52, -7.78, 28.40, 9.0], \ [ 0.0, 0.0, 9.0, 61.0]]) L = choleski(a) print 'L =\n', L print '\nCheck: L*L_transpose =\n', \ matrixmultiply(L,transpose(L)) raw_input("\nPress return to exit")
def rotate(input, angle, axes = (-1, -2), reshape = True, output_type = None, output = None, order = 3, mode = 'constant', cval = 0.0, prefilter = True): """Rotate an array. The array is rotated in the plane defined by the two axes given by the axes parameter using spline interpolation of the requested order. The angle is given in degrees. Points outside the boundaries of the input are filled according to the given mode. If reshape is true, the output shape is adapted so that the input array is contained completely in the output. The parameter prefilter determines if the input is pre- filtered before interpolation, if False it is assumed that the input is already filtered. """ input = numarray.asarray(input) axes = list(axes) rank = input.rank if axes[0] < 0: axes[0] += rank if axes[1] < 0: axes[1] += rank if axes[0] < 0 or axes[1] < 0 or axes[0] > rank or axes[1] > rank: raise RuntimeError, 'invalid rotation plane specified' if axes[0] > axes[1]: axes = axes[1], axes[0] angle = numarray.pi / 180 * angle m11 = math.cos(angle) m12 = math.sin(angle) m21 = -math.sin(angle) m22 = math.cos(angle) matrix = numarray.array([[m11, m12], [m21, m22]], type = numarray.Float64) iy = input.shape[axes[0]] ix = input.shape[axes[1]] if reshape: mtrx = numarray.array([[ m11, -m21], [-m12, m22]], type = numarray.Float64) minc = [0, 0] maxc = [0, 0] coor = numarray.matrixmultiply(mtrx, [0, ix]) minc, maxc = _minmax(coor, minc, maxc) coor = numarray.matrixmultiply(mtrx, [iy, 0]) minc, maxc = _minmax(coor, minc, maxc) coor = numarray.matrixmultiply(mtrx, [iy, ix]) minc, maxc = _minmax(coor, minc, maxc) oy = int(maxc[0] - minc[0] + 0.5) ox = int(maxc[1] - minc[1] + 0.5) else: oy = input.shape[axes[0]] ox = input.shape[axes[1]] offset = numarray.zeros((2,), type = numarray.Float64) offset[0] = float(oy) / 2.0 - 0.5 offset[1] = float(ox) / 2.0 - 0.5 offset = numarray.matrixmultiply(matrix, offset) tmp = numarray.zeros((2,), type = numarray.Float64) tmp[0] = float(iy) / 2.0 - 0.5 tmp[1] = float(ix) / 2.0 - 0.5 offset = tmp - offset output_shape = list(input.shape) output_shape[axes[0]] = oy output_shape[axes[1]] = ox output_shape = tuple(output_shape) output, return_value = _ni_support._get_output(output, input, output_type, shape = output_shape) if input.rank <= 2: affine_transform(input, matrix, offset, output_shape, None, output, order, mode, cval, prefilter) else: coordinates = [] size = input.nelements() size /= input.shape[axes[0]] size /= input.shape[axes[1]] for ii in range(input.rank): if ii not in axes: coordinates.append(0) else: coordinates.append(slice(None, None, None)) iter_axes = range(input.rank) iter_axes.reverse() iter_axes.remove(axes[0]) iter_axes.remove(axes[1]) os = (output_shape[axes[0]], output_shape[axes[1]]) for ii in range(size): ia = input[tuple(coordinates)] oa = output[tuple(coordinates)] affine_transform(ia, matrix, offset, os, None, oa, order, mode, cval, prefilter) for jj in iter_axes: if coordinates[jj] < input.shape[jj] - 1: coordinates[jj] += 1 break else: coordinates[jj] = 0 return return_value
## example9_4 from numarray import array,matrixmultiply,dot from math import sqrt s = array([[-30.0, 10.0, 20.0], \ [ 10.0, 40.0, -50.0], \ [ 20.0, -50.0, -10.0]]) v = array([1.0, 0.0, 0.0]) for i in range(100): vOld = v.copy() z = matrixmultiply(s,v) zMag = sqrt(dot(z,z)) v = z/zMag if dot(vOld,v) < 0.0: sign = -1.0 v = -v else: sign = 1.0 if sqrt(dot(vOld - v,vOld - v)) < 1.0e-6: break lam = sign*zMag print "Number of iterations =",i print "Eigenvalue =",lam raw_input("Press return to exit")
#!/usr/bin/python ## example2_13 from numarray import array,identity, matrixmultiply from LUpivot import * def matInv(a): n = len(a[0]) aInv = identity(n)*1.0 a,seq = LUdecomp(a) for i in range(n): aInv[:,i] = LUsolve(a,aInv[:,i],seq) return aInv a = array([[ 0.6, -0.4, 1.0],\ [-0.3, 0.2, 0.5],\ [ 0.6, -1.0, 0.5]]) aOrig = a.copy() # Save original [a] aInv = matInv(a) # Invert [a] (original [a] is destroyed) print "\naInv =\n",aInv print "\nCheck: a*aInv =\n", matrixmultiply(aOrig,aInv) raw_input("\nPress return to exit")
## example2_4 from numarray import zeros,Float64,array,product, \ diagonal,matrixmultiply from gaussElimin import * def vandermode(v): n = len(v) a = zeros((n, n), type=Float64) for j in range(n): a[:, j] = v**(n - j - 1) return a v = array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0]) b = array([0.0, 1.0, 0.0, 1.0, 0.0, 1.0]) a = vandermode(v) aOrig = a.copy() # Save original matrix bOrig = b.copy() # and the constant vector x = gaussElimin(a, b) det = product(diagonal(a)) print 'x =\n', x print '\ndet =', det print '\nCheck result: [a]{x} - b =\n', \ matrixmultiply(aOrig,x) - bOrig raw_input("\nPress return to exit")
## example2_4 from numarray import zeros,Float64,array,product, \ diagonal,matrixmultiply from gaussElimin import * def vandermode(v): n = len(v) a = zeros((n,n),type=Float64) for j in range(n): a[:,j] = v**(n-j-1) return a v = array([1.0, 1.2, 1.4, 1.6, 1.8, 2.0]) b = array([0.0, 1.0, 0.0, 1.0, 0.0, 1.0]) a = vandermode(v) aOrig = a.copy() # Save original matrix bOrig = b.copy() # and the constant vector x = gaussElimin(a,b) det = product(diagonal(a)) print 'x =\n',x print '\ndet =',det print '\nCheck result: [a]{x} - b =\n', \ matrixmultiply(aOrig,x) - bOrig raw_input("\nPress return to exit")