Absolute value of matrices

1,093 views
Skip to first unread message

pong

unread,
Apr 4, 2011, 6:00:20 PM4/4/11
to sage-support
By that I simply mean a function that on input a real matrix M returns
the matrix N such that n[i][j] = abs(m[i][j]).

This can be achieve by something like:

n = len(M.rows()); m =len(M.columns()); N = matrix(n,m,lambda i,j:
abs(M[i][j]));

However, for a square matrix M, M.abs() returns something which wasn't
what one expected:

B = matrix(2,2,lambda i,j: i-j); B; B.abs()

returns

[ 0 -1]
[ 1 0]

and 1

Is it a bug? Or something that I missed?

John H Palmieri

unread,
Apr 4, 2011, 6:19:13 PM4/4/11
to sage-s...@googlegroups.com

For matrices, B.abs() returns the determinant.  If you type "B.abs?", you'll see a message like

       Return the absolute value of self.  (This just calls the __abs__
       method, so it is equivalent to the abs() built-in function.)

Then if you type "B.__abs__?", you'll see
   
       Synonym for self.determinant(...).

--
John
 

Mike Hansen

unread,
Apr 4, 2011, 6:30:51 PM4/4/11
to sage-s...@googlegroups.com, pong
Hello,

On Tue, Apr 5, 2011 at 12:00 AM, pong <wypo...@gmail.com> wrote:
> By that I simply mean a function that on input a real matrix M returns
> the matrix N such that n[i][j] = abs(m[i][j]).
>
> This can be achieve by something like:
>
> n = len(M.rows()); m =len(M.columns()); N = matrix(n,m,lambda i,j:
> abs(M[i][j]));

A bit cleaner is:

sage: B = matrix(2,2,lambda i,j: i-j); B
[ 0 -1]
[ 1 0]
sage: B.apply_map(abs)
[0 1]
[1 0]

--Mike

Justin C. Walker

unread,
Apr 4, 2011, 8:35:39 PM4/4/11
to sage-s...@googlegroups.com

I suppose this is because the determinant is sometimes written as
|1 0|
|0 -1|
but I think that's carrying things too far. I'd say this violates the Principle of Least Surprise...

I see two "bugs": that introspection claims that ".abs()" is defined in the file it claims:
=======================
sage: B.abs?
String Form: <built-in method abs of sage.matrix.matrix_integer_dense.Matrix_integer_dense object at 0x10ce3a4d0>
Namespace: Interactive
Definition: B.abs(self)
Docstring:



Return the absolute value of self. (This just calls the __abs__
method, so it is equivalent to the abs() built-in function.)

=======================

(which is not the case); and the use of "abs" (in any form) for determinant.

But that's just me.

Justin

--
Justin C. Walker
Curmudgeon-at-large
Director
Institute for the Absorption of Federal Funds
----
186,000 Miles per Second
Not just a good idea:
it's the law!
----

John Cremona

unread,
Apr 6, 2011, 11:39:07 PM4/6/11
to sage-support
I agree. It makes no sense at all to me for A.abs() to return the
determinant of A.

For real or complex matrices it would make sense for A.abs() to be
sqrt(trace(A^*A)) where A^* is the conjugate transpose. This is just
the square root of the sums of the squares of the absolute values of
the entries, i.e. the standard Hermitian norm.

John

Keshav Kini

unread,
Apr 7, 2011, 2:05:52 AM4/7/11
to sage-s...@googlegroups.com
Indeed, this seems very reasonable. It might be better to implement it separately, though, since computing A^*A might take much longer than just squaring the elements and adding them, to get trace(A^*A).

-Keshav

Rob Beezer

unread,
Apr 7, 2011, 11:56:12 AM4/7/11
to sage-support
On Apr 6, 8:39 pm, John Cremona <john.crem...@gmail.com> wrote:
> where A^* is the conjugate transpose.  

You mean the "adjoint of a matrix", right? ;-)

I hijacked this topic and regenerated it over on sage-devel - should
have posted a link earlier:

http://groups.google.com/group/sage-devel/browse_thread/thread/86329fb75a2abc64
Reply all
Reply to author
Forward
0 new messages