Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugix for Frobenius norm of complex matrices. #1098

Merged
merged 1 commit into from May 5, 2018

Conversation

jackschmidt
Copy link
Contributor

As mentioned in #1094

The code on line 195 of lib/function/arithmetic/norm.js is comment with sqrt(sum(diag(x'x))) but this is only true for the conjugate transpose. The general formula is sqrt(sum(sum(pow(abs(a[i,j]),2),i=1..m),j=1..n)), the square root of the sum of the absolute values squared of the entries.

This commit makes mathjs agree with matlab, and improves the efficiency by directly summing a[i,j]' * a[i,j]. The old method was cubic (for square matrices); this method is quadratic. I also added a short test.

In mathjs 5.2.1:

math.eval('norm([[3+4i]],"fro")')
{ re: 3, im: 4 }

In matlab:

norm([[3+4i]],'fro')
ans =  5

After this commit, mathjs outputs 5.

Also improve efficiency from cubic to quadratic by avoiding taking
the trace of a mat-mat multiplication, and rather just summing the
formula for the diagonal entries.

Include a unit test to avoid regressions.
Copy link
Collaborator

@harrysarson harrysarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

This should also speed up the frobius norm a fair bit as it no longer multiplies matricies and throws away all the non diagonal entries. :)

@josdejong
Copy link
Owner

Thanks @jackschmidt for finding this bug!

@josdejong josdejong merged commit edfdc7f into josdejong:develop May 5, 2018
@jackschmidt jackschmidt deleted the frob-bugfix branch May 5, 2018 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants