-4

I hope if someone knows how to express the following Mupad code in Python:

for n from 1 to 6 do
 M:= matrix([[B(k,j) $ k = 0..n] $ j = 0..n]);
 C:=matrix([c(j)$ j = 0..n]);
 A:=linalg::matlinsolve(M, C);P:=sum(A[j+1]*x^j, j = 0..n);plot(P,fx, x =-1..1);
end_for:
  • 2
    That looks like very complicated piece of code. It's been squashed down to take very little space and doesn't use descriptive variables. Why do you need this translated? – Bluenix Mar 23 '21 at 20:55
  • Indeed, this is part of a method of Least Squares approximation for a function, currently I'm trying to construct a different kind of code, because this one is hard to read, thanks @Bluenix – Daniel Vijil Mar 23 '21 at 22:35

1 Answers1

0

I don't understand this code at all, but here's some tips...

For-loops in Python look like this (this is Python shell):

>>> for i in range(1, 6+1):
...     print(i)
...
1
2
3
4
5
6

Not knowing Mupad I am unsure if for n from 1 to 6 do means:

a) 0, 1, 2, 3, 4, 5 (range(0, 6))

b) 0, 1, 2, 3, 4, 5, 6 (range(0, 6+1))

c) 1, 2, 3, 4, 5, 6 (range(1, 6+1))

I see that there's references of "matrix", for that see the commonly used library NumPy and their documentation on numpy.matrix. It already has a lot of methods for your simplicity.

Your best option is probably to search up the NumPy alternative rather than do the work with this piece of code.

Bluenix
  • 409
  • 4
  • 11