当前位置: 首页>>代码示例>>Python>>正文


Python Matrix.add方法代码示例

本文整理汇总了Python中Matrix.Matrix.add方法的典型用法代码示例。如果您正苦于以下问题:Python Matrix.add方法的具体用法?Python Matrix.add怎么用?Python Matrix.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Matrix.Matrix的用法示例。


在下文中一共展示了Matrix.add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: cg

# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import add [as 别名]
def cg(A,b):
	#guess x all 0s
	x = Matrix(i=A.columns,j=1)

	#set r and p
	r = b.subtract(A.multiply(x))
	p = b.subtract(A.multiply(x))
	
	r_norm_inf = 0
	for i in range(1,r.rows+1):
		v = r.get(i,1)
		if (v > r_norm_inf):
			r_norm_inf = v
	r_norm_2 = 0
	r_norm_2 = math.sqrt(r.transpose().multiply(r).get(1,1))
	
	iteration = 1
	
	while( r_norm_2 > LIMITERROR):
		p_t = p.transpose()
		alpha = p_t.multiply(r).get(1,1) /  p_t.multiply(A.multiply(p)).get(1,1)
		
		
		# a_p for alpha*p
		a_p = copy.deepcopy(p)
		for i in range(1,a_p.rows+1):
			for j in range(1,a_p.columns+1):
				a_p.set(i,j,a_p.get(i,j)*alpha)
		
		x = x.add(a_p)
		
		r = b.subtract(A.multiply(x))
		beta = -1 * (p_t.multiply(A.multiply(r)).get(1,1) / p_t.multiply(A.multiply(p)).get(1,1))

		
		# b_p for beta*p
		b_p = p #no need to copy (as we update cell by cell of p to b_p and we don't need it later)
		for i in range(1,b_p.rows+1):
			for j in range(1,b_p.columns+1):
				b_p.set(i,j,p.get(i,j)*beta)
		p = r.add(b_p)
		
		# compute r norms and f.write
		r_norm_inf = 0
		for i in range(1,r.rows+1):
			v = r.get(i,1)
			if (v > r_norm_inf):
				r_norm_inf = v
		
		r_norm_2 = math.sqrt(r.transpose().multiply(r).get(1,1))
		
		f.write(str(iteration)+","+str(r_norm_inf)+","+str(r_norm_2)+"\n")
		iteration+=1
	
	f.write(",,,"+str(iteration-1))
	return x
开发者ID:AkeelAli,项目名称:Numerical,代码行数:58,代码来源:CG.py

示例2: __init__

# 需要导入模块: from Matrix import Matrix [as 别名]
# 或者: from Matrix.Matrix import add [as 别名]
class Operations:
    def __init__(self):
        self.data = []
        self.matrices = []
        self.mean = Matrix([[0, 0]])
        self.covariance = Matrix([[0, 0], [0, 0]])
        self.setup()
        self.set_mean()
        self.set_covariance()
        print(len(self.matrices))

    def get_mean(self):
        return self.mean

    def get_covariance(self):
        return self.covariance

    # Read in and prepare the eigendata from a file
    def setup(self):
        temp = []
        filename = open('eigendata.txt', 'r')
        for line in filename:
            row = line.strip()
            temp.append(row)
        for i in range(0, len(temp)):
            self.data.append(temp[i].split())
        for i in self.data:
            matrix = Matrix([[float(i[0]), float(i[1])]])
            self.matrices.append(matrix)

    # Calculate the mean and assign it to the Class variable self.mean
    def set_mean(self):
        for i in self.matrices:
            self.mean = self.mean.add(i)

        self.mean.scaler(1 / len(self.matrices))

    # Calculate the covariance and assign it to the Class variable self.covariance
    def set_covariance(self):
        for i in self.matrices:
            a = i.subtract(self.mean)
            a_transpose = Matrix(a.get_data())
            a_transpose.transpose()
            b = a_transpose.multiply(a)
            self.covariance = self.covariance.add(b)

        self.covariance.scaler(1 / len(self.matrices))

    # Implementation of the power method for finding the dominant eigenvalue for a square matrix
    # Returns both eigenvalues for a 2x2 matrix as well as a unit length vector.
    # If the matrix is not a 2x2 matrix mu will be the dominant eigenvalue.
    # iterations: max number of iterations allowed
    # mat: the covariance matrix of the matrix being tested
    # estimate: a matrix to begin with as the estimate
    def power(self, iterations, mat, estimate):
        r = 1
        k = 0
        m = iterations
        y = estimate
        x = mat.multiply(y)
        while r > .001 and k < m:
            max_x = x.find_max()
            x.scaler(1 / max_x)
            y = Matrix(x.get_data())
            x = mat.multiply(y)
            temp = Matrix(y.get_data())
            temp.transpose()
            a = temp.multiply(x).get_data()[0][0]
            b = temp.multiply(y).get_data()[0][0]
            mu = a / b
            r = Matrix(y.get_data())
            r.scaler(mu)
            r = r.subtract(x)
            r = r.find_max()
            k += 1

        y.scaler(1 / mu)

        return mu, mat.trace() - mu, y

    # Implementation of the leverrier method for finding the characteristic equation for a polynomial
    def leverrier(self, companion):
        res = []
        b = companion
        a = -(b.trace())
        res.append(a)
        for i in range(companion.get_rows() - 1, 0, -1):
            ai = companion.identity()
            ai.scaler(a)
            b = companion.multiply(b.add(ai))
            a = -(b.trace()) / (5 - i + 1)
            res.append(a)
        return res

    # This method is used to find the roots of a polynomial equation by providing the companion matrix
    # for the polynomial and an estimate matrix for the power method.
    # Based on Deflation from this site: http://www.maths.qmul.ac.uk/~wj/MTH5110/notes/notes08.pdf
    def find_roots(self, comp, est):
        count = comp.get_rows()
        for i in range(count):
#.........这里部分代码省略.........
开发者ID:darryl-papke,项目名称:340-Code,代码行数:103,代码来源:Operations.py


注:本文中的Matrix.Matrix.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。