.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/scipy_sparse_matrix.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_scipy_sparse_matrix.py: AnsMath sparse matrices and SciPy sparse matrices ------------------------------------------------- This example shows how to get AnsMath sparse matrices into SciPy sparse matrices. .. GENERATED FROM PYTHON SOURCE LINES 32-35 Perform required imports and start PyAnsys ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Perform required imports. .. GENERATED FROM PYTHON SOURCE LINES 35-44 .. code-block:: Python from ansys.mapdl.core.examples import vmfiles import matplotlib.pylab as plt import ansys.math.core.math as pymath # Start PyAnsys Math as a service. mm = pymath.AnsMath() .. GENERATED FROM PYTHON SOURCE LINES 45-49 Get matrices ~~~~~~~~~~~~ Run the input file from Verification Manual 153 and then get the stiff (``k``) matrix from the FULL file. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: Python out = mm._mapdl.input(vmfiles["vm153"]) fullfile = mm._mapdl.jobname + ".full" k = mm.stiff(fname=fullfile) .. GENERATED FROM PYTHON SOURCE LINES 55-59 Copy AnsMath sparse matrix to SciPy CSR matrix and plot ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Copy the AnsMath sparse matrix to a SciPy CSR matrix. Then, plot the graph of the sparse matrix. .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. code-block:: Python pk = k.asarray() plt.spy(pk, color="orange", markersize=3) plt.title("AnsMath sparse matrix") plt.show() .. image-sg:: /examples/images/sphx_glr_scipy_sparse_matrix_001.png :alt: AnsMath sparse matrix :srcset: /examples/images/sphx_glr_scipy_sparse_matrix_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 67-78 Access vectors ~~~~~~~~~~~~~~ You can access the three vectors that describe this sparse matrix with: - ``pk.data`` - ``pk.indices`` - ``pk.indptr`` For more information, see SciPy's class description for the `CSR (compressed sparse row) matrix `_. .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: Python print(pk.data[:10]) print(pk.indices[:10]) print(pk.indptr[:10]) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0.20292539 0.00378143 -0.05003569 -0.01392161 -0.05003569 -0.01392161 -0.05142701 0.02406179 -0.05142701 0.20090661] [ 0 1 4 7 18 21 30 47 50 1] [ 0 9 17 29 40 47 57 66 72 79] .. GENERATED FROM PYTHON SOURCE LINES 85-92 Create AnsMath sparse matrix from SciPy sparse CSR matrix ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create an AnsMath sparse matrix from a SciPy sparse CSR matrix. Then, transfer the SciPy CSR matrix back to PyAnsys Math. While this code uses a matrix that was originally within MAPDL, you can load any CSR matrix into PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 92-96 .. code-block:: Python my_mat = mm.matrix(pk, "my_mat", triu=True) my_mat .. rst-class:: sphx-glr-script-out .. code-block:: none AnsMath sparse matrix (63, 63) .. GENERATED FROM PYTHON SOURCE LINES 97-99 Check that the matrices ``k`` and ``my_mat`` are exactly the sames. The norm of the difference should be zero. .. GENERATED FROM PYTHON SOURCE LINES 99-104 .. code-block:: Python msub = k - my_mat mm.norm(msub) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 105-117 Print CSR representation in PyAnsys Math ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Printing the list of objects for the CSR representation in the PyAnsys Math space finds these objects: - Two SMAT objects, corresponding to the ``k``, ``MSub`` matrices, with encrypted names. - The ``my_mat`` SMAT object. Its size is zero because the three vectors are stored separately. - The three vectors of the CSR ``my_mat`` structure: ``MY_MAT_PTR``, ``MY_MAT_IND``, and ``MY_MAT_DATA``. .. GENERATED FROM PYTHON SOURCE LINES 117-121 .. code-block:: Python mm.status() .. rst-class:: sphx-glr-script-out .. code-block:: none APDLMATH PARAMETER STATUS- ( 6 PARAMETERS DEFINED) Name Type Mem. (MB) Dims Workspace MY_MAT SMAT 0.000 [63:63] 1 VBHKYR SMAT 0.006 [63:63] 1 VXDWOF SMAT 0.006 [63:63] 1 MY_MAT_DATA VEC 0.003 344 1 MY_MAT_IND VEC 0.000 64 1 MY_MAT_PTR VEC 0.001 344 1 .. GENERATED FROM PYTHON SOURCE LINES 122-127 Access ID of Python object ~~~~~~~~~~~~~~~~~~~~~~~~~~ To determine which PyAnsys Math object corresponds to which Python object, access the ``id`` property of the Python object. .. GENERATED FROM PYTHON SOURCE LINES 127-133 .. code-block:: Python print("name(k)=" + k.id) print("name(my_mat)=" + my_mat.id) print("name(msub)=" + msub.id) .. rst-class:: sphx-glr-script-out .. code-block:: none name(k)=VXDWOF name(my_mat)=my_mat name(msub)=VBHKYR .. GENERATED FROM PYTHON SOURCE LINES 134-137 Stop PyAnsys Math ~~~~~~~~~~~~~~~~~ Stop PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 137-139 .. code-block:: Python mm._mapdl.exit() .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/ansys/mapdl/core/launcher.py:811: UserWarning: The environment variable 'PYMAPDL_START_INSTANCE' is set, hence the argument 'start_instance' is overwritten. warnings.warn( .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.594 seconds) .. _sphx_glr_download_examples_scipy_sparse_matrix.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: scipy_sparse_matrix.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: scipy_sparse_matrix.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_