Wolfram Research

Function Repository Resource:

SciPyObject (1.0.0) current version: 1.1.0 »

Source Notebook

A minimal PythonObject configuration for the SciPy package

Contributed by: Igor Bakshee

ResourceFunction["PythonObject"][,"Configuration"ResourceFunction["SciPyObject"]]

returns a PythonObject with a special configuration for the Python package SciPy.

Details

The Python package SciPy provides algorithms for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics and other classes of problems.
ResourceFunction["SciPyObject"] sets up a configuration of the resource function PythonObject that makes working with the Python package more convenient by generating SparseArray objects in the results.
At this time, ResourceFunction["SciPyObject"] is rudimentary and limited to only providing a single Normal form of a Python object.

Examples

Basic Examples (5) 

Create a Python object for the SciPy's compressed sparse row matrix:

In[1]:=
csr = ResourceFunction["PythonObject"]["csr_matrix", "Configuration" -> ResourceFunction["SciPyObject"], Initialization -> "from scipy.sparse import csr_matrix"]
Out[1]=

Define a sparse matrix in Python:

In[2]:=
csr[All][IdentityMatrix[{5, 10}]]
Out[2]=

Convert the Python object to SparseArray:

In[3]:=
% // Normal
Out[3]=

Display in the dense form:

In[4]:=
Normal[%] // TraditionalForm
Out[37]=

Clean up by closing the Python session:

In[38]:=
DeleteObject[csr["Session"]]

Applications (6) 

The resource function ProbNumObject uses the SciPyObject configuration for sparse matrices:

In[39]:=
p = ResourceFunction["ProbNumObject"][]
Out[39]=

Use Python's random number generator:

In[40]:=
rng = ResourceFunction["ProbNumObject"][p, "RNG"[42]]
Out[40]=

Create a random sparse symmetric positive definite matrix with ProbNumObject:

In[41]:=
spd = p["RandomSparseSPDMatrix"[rng, 5, .1]]
Out[41]=

Convert the Python-side matrix to SparseArray:

In[42]:=
spdn = Normal[spd]
Out[42]=

Display in the dense form:

In[43]:=
Normal[%] // MatrixForm
Out[89]=

Clean up by closing the Python session:

In[90]:=
DeleteObject[p["Session"]]

Version History

  • 1.1.0 – 14 February 2024
  • 1.0.0 – 15 March 2022

Related Resources

Author Notes

In its current form, this function is mainly a placeholder, but it is already used in ProbNumObject.

License Information