Ask Your Question
0

A problem with pseudo inverse

asked 2023-02-09 11:57:41 +0100

Cyrille gravatar image

I have defined the following function

 def pseudo_inverse(mat) :
        from scipy import linalg
         return matrix(linalg.pinv(mat))

From the eye it works correctly. But if I apply it to

M = matrix(RR,2,3,(1,2,2,4,5,6))

that is

M+=pseudo_inverse(M)
M*M+*M == M

returns False (when I can see that it is formaly true but not operationaly). I think I understand where is the problem but even in changing the ring ( with the help of the function change_ring() I cannot obtain the desired identity.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-02-09 18:13:20 +0100

Max Alekseyev gravatar image

You cannot use M+ as a variable name. In fact, M+=... is interpreted as incrementing M by what is given in the right hand side. Try this out:

Mp = pseudo_inverse(M)
M * Mp * M == M
edit flag offensive delete link more

Comments

1

Additionally, you should not check the equality of real numbers. You should check if their absolute difference is smaller than the error limit that you will define (for example, 10^-7). You can display M*Mp*M and M to see what I mean.

tolga gravatar imagetolga ( 2023-02-10 07:08:11 +0100 )edit
3

To complement @tolga's remark :

RR is not $\mathbb{R}$, but a (pseudo-) ring of floating-point approximations. Example :

sage: 3.17.parent()
Real Field with 53 bits of precision

The best you can do to declare a variable as real without giving it a value is to declare it as such. Compare :

sage: sqrt(x^2).simplify()
sqrt(x^2)
sage: var("y", domain="real")
y
sage: assumptions()
[y is real]
sage: sqrt(y^2).simplify()
abs(y)

For numerical values, AA allows for exact representation of algebraic values. Compare :

sage: cos(pi/6)
1/2*sqrt(3)
sage: AA(cos(pi/6))
0.866025403784439?
sage: RR(cos(pi/6))
0.866025403784439

The ? ending the representation of the AA value denotes the "arbitrary precision".

HTH

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-02-10 08:32:39 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-02-09 11:57:41 +0100

Seen: 252 times

Last updated: Feb 09 '23