Skip to content

Conversation

@rocky
Copy link
Member

@rocky rocky commented Jan 17, 2021

Add option to compute Eigenvectors with mpmath

We should add this option more pervasively.

When converting from mpmath complex, convert to real number
when imag is 0.

We should also consider converting to int's too when possible.

rocky added 2 commits January 17, 2021 11:37
We should add this option more pervasively.

When converting from mpmath complex, convert to real number
when imag is 0.

We should also consider converting to int's too when possible.
@rocky rocky requested review from GarkGarcia and mmatera January 17, 2021 16:52
'Eigenvalues[m_, OptionsPattern[Eigenvalues]]'

method = self.get_option(options, 'Method', evaluation)
if method and method.get_string_value() == "mpmath":
Copy link
Contributor

Choose a reason for hiding this comment

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

If you replace the string with a symbol, we should use method.has_form in here

Compute Eigenvalues using mpmath's routines; Sympy is slow here and returns
complex numbers.
>> Eigenvalues[{{-8, 12, 4}, {12, -20, 0}, {4, 0, -2}}, Method->"mpmath"]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd consider using a symbol to indicate the Method instead of a string (I think this is more consistent with the rest of the library, but I could be wrong). So I'd replace "mpmath" with MPMath.

Copy link
Member Author

Choose a reason for hiding this comment

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

I got this idea from WL's Eigenvector. It uses strings for implementation options. I was thinking of "mpmath" more akin to "Arnoldi" ,"Banded", or "FEAST" rather than Automatic.

When I grep for "Method" right now in mathics/builtin, I am not coming up with any instances where there is a Symbol used, just strings:

image.py:    >> Threshold[img, Method -> "Mean"]
image.py:    >> Threshold[img, Method -> "Median"]
image.py:    options = {"Method": '"Cluster"'}
lists.py:        "bdmtd": 'Method in `` must be either "Optimize", "Agglomerate" or "KMeans".',

Copy link
Contributor

Choose a reason for hiding this comment

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

I got this idea from WL's Eigenvector. It uses strings for implementation options. I was thinking of "mpmath" more akin to "Arnoldi" ,"Banded", or "FEAST" rather than Automatic.

When I grep for "Method" right now in mathics/builtin, I am not coming up with any instances where there is a Symbol used, just strings:

image.py:    >> Threshold[img, Method -> "Mean"]
image.py:    >> Threshold[img, Method -> "Median"]
image.py:    options = {"Method": '"Cluster"'}
lists.py:        "bdmtd": 'Method in `` must be either "Optimize", "Agglomerate" or "KMeans".',

My bad, strings are the right choice then.

if matrix is None:
eigenvalues = ER.tolist()
# Sort the eigenvalues in the Mathematica convention: largest first.
eigenvalues.sort(key=lambda v: (abs(v[0]),-v[0].real,-(v[0].imag)), reverse=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably extract this (the sorting routine) to a separate function to avoid duplication of code (so that we don't forget to update both lines if we want to update this in the future). This should be inside of a try block too.

@mmatera
Copy link
Contributor

mmatera commented Jan 19, 2021

Some thoughts:

  • It is great to start having support for numeric Linear Algebra!
  • If you do not specify accuracy, WMA uses machine-precision numbers, and machine-precisions routines like those you find in numpy. For large matrices, these routines are by far faster than those from the arbitrary precision library. In any case, we can use this as an starting point
  • In WMA, the choice of the set of routines is defined by the class of numbers in the matrix. If there are symbolic expressions as entries, or all the elements are exact numbers (Integers/Rationals/Algebraic numbers) then it uses the algebraic approach. On the other hand, if the matrix is numeric, and at least one entry is a machine-precision number, then it uses machine-precision routines (BLAS/LAPACK). If there are not machine-precision entries, but at least there is a single arbitrary-precision number, it uses the arbitrary-precision routine with precision fixed by the entry with the lowest precision. I guess we could implement this without a huge effort.

@mmatera
Copy link
Contributor

mmatera commented Jan 20, 2021

Regarding what to use for Options (like method) the idea behind using Strings instead of symbols is to avoid name collisions. Also, notice that is a good practice to use Strings for the name of the options: WL neglects the context of the symbol, so the only way for avoiding name collisions in option names is to use Strings.

@rocky rocky merged commit 0b30e3e into master Jan 23, 2021
@rocky rocky deleted the mpmath-eigenvalues branch February 14, 2021 18:17
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.

4 participants