sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.32k stars 451 forks source link

make solve understand matrix equations #5201

Open jasongrout opened 15 years ago

jasongrout commented 15 years ago

I think it would be a great thing if solve could recognize matrices and that two matrices are equal if each entry is equal. I believe MMA does this (but it's easier there; matrices are nothing more than nested lists). It'd certainly make certain things I do more natural if I could do:

solve(matrixA==matrixB)

and that was equivalent to:

solve([i==j for i,j in zip(matrixA.list(), matrixB.list())])

Okay, so now that I've written my piece, I suppose the next step is to open a trac ticket, write a patch to implement it, and post it for review :).

Component: calculus

Issue created by migration from https://trac.sagemath.org/ticket/5201

85eec1a4-3d04-4b4d-b711-d4db03337c41 commented 15 years ago
comment:1

Which part of no non-ReST tickets against 3.4 is hard to understand? :p

Cheers,

Michael

jasongrout commented 15 years ago
comment:2

argh! I looked at the list and thought "the first item is the ReST transition, so I have to pick the second item". Apparently I was thinking that the next release was already out and 3.3 was the ReST transition.

cb31b68b-3181-4f46-ab62-081a947f4434 commented 13 years ago
comment:4

Could this be accomplished by overriding the comparison operator for the matrix class?

for example

def __richcmp__(self, other_matrix, cmptype):
  if cmptype == 2:  #this is the '==' operator
    if is_Matrix(other_matrix):
      if False in [i==j for i,j in zip(self.list(), other_matrix.list())]:
        return False
      else: return True

I'm just not sure where the 'matrix class' is. This would allow comparisons like

sage: matrixA == matrixB
True
jasongrout commented 13 years ago
comment:5

You bring up a good point, and make me doubt whether the feature request is even feasible. Certainly it's probably not a beginner ticket after all. The problem is that we already have an == operator:

sage: a=matrix(SR,2,[x,x^2,x+1,x+4])
sage: b=matrix(SR,2,[4,3,2,1])
sage: a==b
False

That means that all solve will see is False. Instead, we want something like:


sage: SR(a)==SR(b)
([    x   x^2]
[x + 1 x + 4]) == ([4 3]
[2 1])

(i.e., we want the == in the solve to construct an equation, which it does for symbolic objects. One of the issues at heart here is that a symbolic object wrapping a Sage matrix is different from a Sage matrix containing symbolic objects.

So I'm going to take off beginner status for this ticket here. It would still be nice if solve(SR(a)==SR(b)) worked in the above example.