0

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.

2
  • 2
    Correct the indentation. Use import numpy as np. Show what you get and what you expect. Commented Oct 12, 2015 at 9:11
  • Sorry but your code is not clear as your indentation has not come over from your cut & paste. Also, it would be good if you gave us sample input & output with the expected results Commented Oct 12, 2015 at 16:11

1 Answer 1

1

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 
Sign up to request clarification or add additional context in comments.

9 Comments

You mean to do this: p = M.I * x and then return p? No, that does not help.
What are you trying to compute, what is your expected result and what is your actual result? You just throw code at us and says "See? There's a problem!" How am I supposed to know what's wrong if I don't know what's right?
I said what is right, it is result in Mathcad. The problem is with multiplication itself.
I know python, not mathcad. Is "result" a function you try to mimic? A link to what you refer? By the way, all you said is "We were given an example in Mathcad" and "I need to find some coefficients". An example of what? Coefficients of what?
An example of same function. Coefficients of chebyshev approximation used in calculation uf hyperbolic arcsine.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.