Python:矩阵乘法错误

时间:2015-07-08 23:37:23

标签: python numpy scipy sparse-matrix matrix-multiplication

我使用scipy.sparse.diags函数找到了两个矩阵(质量和刚度),并从另一个中取出一个(质量 - 刚度)。当试图将这个新矩阵乘以向量U0时,我得到以下

[ <6x6 sparse matrix of type '<type 'numpy.float64'>'
with 16 stored elements in Compressed Sparse Row format>
 <6x6 sparse matrix of type '<type 'numpy.float64'>'
with 16 stored elements in Compressed Sparse Row format>
 <6x6 sparse matrix of type '<type 'numpy.float64'>'
with 16 stored elements in Compressed Sparse Row format>
 <6x6 sparse matrix of type '<type 'numpy.float64'>'
with 16 stored elements in Compressed Sparse Row format>
 <6x6 sparse matrix of type '<type 'numpy.float64'>'
with 16 stored elements in Compressed Sparse Row format>
 <6x6 sparse matrix of type '<type 'numpy.float64'>'
with 16 stored elements in Compressed Sparse Row format>]

我真的不明白这个结果 - 我是否得到了一个包含6个矩阵的一维数组?如果是这样,为什么我得到这个而不是一个简单的向量?

以下是创建质量/刚度矩阵的代码,找到U0然后找点积

import numpy as np
import math
import scipy
from scipy.sparse import diags
import scipy.sparse.linalg
import matplotlib
import matplotlib.pyplot as plt


def Mass_Matrix(x0):
    """Finds the Mass matrix for any non uniform mesh x0"""
    x0 = np.array(x0)
    N = len(x0) - 1
    h = x0[1:] - x0[:-1]
    a = np.zeros(N+1)
    a[0] = 1
    for j in range(1,N):
        a[j] = h[j-1]/3 + h[j]/3
    a[N] = 1
    b = h/6
    b[0] = 0
    c = h/6
    c[-1] = 0
    data = [a.tolist(), b.tolist(), c.tolist()]
    Positions = [0,1,-1]
    Mass_Matrix = diags(data, Positions, (N+1,N+1))

    return Mass_Matrix

def Poisson_Stiffness(x0):
    """Finds the Poisson equation stiffness matrix with any nonuniform mesh x0"""
    x0 = np.array(x0)
    N = len(x0) - 1 # The amount of elements; x0, x1, ..., xN
    h = x0[1:] - x0[:-1]
    a = np.zeros(N+1)
    a[0] = 1/h[0]
    a[1:-1] = 1/h[1:] + 1/h[:-1]
    a[-1] = 1/h[-1]
    a[N] = 1/h[N-1]
    b = -1/h
    c = -1/h
    data = [a.tolist(), b.tolist(), c.tolist()]
    Positions = [0, 1, -1]
    Stiffness_Matrix = diags(data, Positions, (N+1,N+1))

    return Stiffness_Matrix

def Nodal_Quadrature_f(x0):
    """Finds the Nodal Quadrature Approximation of sin(pi x)"""
    x0 = np.array(x0)
    h = x0[1:] - x0[:-1]
    N = len(x0) - 1
    approx = np.zeros(len(x0))
    for i in range(1,N):
        approx[i] = math.sin(math.pi*x0[i])
        approx[i] = (approx[i]*h[i-1] + approx[i]*h[i])/2

    return approx

def Initial_U(x0): 
    """Finds the initial U0"""
    x0 = np.array(x0)
    h = x0[1:] - x0[:-1]
    N = len(x0) - 1
    Mass = Mass_Matrix(x0)
    u0phi = Nodal_Quadrature_f(x0)
    U0 = scipy.sparse.linalg.spsolve(Mass, u0phi)

    return U0

def timestepping(x0,theta,T,m):

    x0 = np.array(x0)
    h = x0[1:] - x0[:-1]
    N = len(x0) - 1
    dt = T/m
    U0 = np.array(Initial_U(x0))
    Mass = Mass_Matrix(x0)
    Stiffness = Poisson_Stiffness(x0)
    Iteration =  np.dot(Mass - Stiffness,U0)

    print Iteration

我会感激任何帮助,因为我已经坚持了几个小时。

编辑:一些补充信息

我打电话后收到了来自顶部的6x6矩阵 时间步((0,0.2,0.5,0.6,0.9,1),1,1,5)

即。 x0只是doman 0到1的一个分区.Theta,T和m都是无关紧要的,因为我还没有将它添加到函数中。

0 个答案:

没有答案