MoDzeus
MoDzeus

Reputation: 441

Matrix multiplication with Python

I have a numerical analysis assignment and I need to find some coefficients by multiplying matrices. We were given an example in Mathcad, but now we have to do it in another programming language so I chose Python.

The problem is, that I get different results by multiplying matrices in respective environments. Here's the function in Python:

    from numpy import *
def matrica(C, n):
N = len(C) - 1
m = N - n
A = [[0] * (N + 1) for i in range(N+1)]
A[0][0] = 1
for i in range(0, n + 1):
    A[i][i] = 1
for j in range(1, m + 1):
    for i in range(0, N + 1):
        if i + j <= N:
            A[i+j][n+j] = A[i+j][n+j] - C[i]/2
        A[int(abs(i - j))][n+j] = A[int(abs(i - j))][n+j] - C[i]/2
M = matrix(A)
x = matrix([[x] for x in C])
return [float(y) for y in M.I * x]

As you can see I am using numpy library. This function is consistent with its analog in Mathcad until return statement, the part where matrices are multiplied, to be more specific. One more observation: this function returns correct matrix if N = 1.

Upvotes: 0

Views: 883

Answers (1)

L&#230;rne
L&#230;rne

Reputation: 3142

I'm not sure I understand exactly what your code do. Could you explain a little more, like what math stuff you're actually computing. But if you want a plain regular product and if you use a numpy.matrix, why don't you use the already written matrix product?

a = numpy.matrix(...)
b = numpy.matrix(...)
p = a * b #matrix product

Upvotes: 1

Related Questions