# APDLMath Sparse Matrices and SciPy Sparse Matrices#

This tutorial will show how to get APDLMath sparse matrices (typically stiffness or mass matrices extracted from APDL .Full Files) into SciPy Sparse Matrices.

```import matplotlib.pylab as plt

from ansys.mapdl.core import launch_mapdl
from ansys.mapdl.core.examples import vmfiles

mapdl = launch_mapdl()
mm = mapdl.math
```

Load and solve verification manual example 153. Then load the stiffness matrix into APDLmath.

```out = mapdl.input(vmfiles["vm153"])
k = mm.stiff(fname="PRSMEMB.full")
k
```

Out:

```Sparse APDLMath Matrix (126, 126)
```

Copy this APDLMath Sparse Matrix to a SciPy CSR matrix and plot the graph of the sparse matrix

```pk = k.asarray()
plt.spy(pk)
``` Out:

```<matplotlib.lines.Line2D object at 0x7f6c145acac0>
```

You can access the 3 vectors that describe this sparse matrix with.

• `pk.data`

• `pk.indices`

• `pk.indptr`

See the `scipy` documentation of the csr matrix at scipy.sparse.csr_matrix for additional details.

```print(pk.data[:10])
print(pk.indices[:10])
print(pk.indptr[:10])
```

Out:

```[ 0.57249304  0.56369167 -0.28624652 -0.28184583 -0.24789676 -0.24408565
-0.14312326 -0.14092292  0.77576289 -0.37033122]
[ 0  1  4  7 10 13 73 76  1  4]
[ 0  8 19 31 42 49 55 60 63 71]
```

### Create a APDLMath Sparse Matrix from a SciPy Sparse CSR Matrix

Here, we transfer the `scipy` CSR matrix back to MAPDL. While this example uses a matrix that was originally within MAPDL, you can load any CSR matrix to MAPDL.

```my_mat = mm.matrix(pk, "my_mat", triu=True)
my_mat
```

Out:

```Sparse APDLMath Matrix (126, 126)
```

Check initial matrix `k` and `my_mat` are exactly the sames: We compute the norm of the difference, should be zero

```msub = k - my_mat
mm.norm(msub)
```

Out:

```0.0
```

## CSR Representation in MAPDL#

Printing the list of objects in the MAPDL space, we find:

• 2 SMAT objects, corresponding to the `k`, `MSub` matrices,

• with encrypted names

• The `my_mat` SMAT object. Its size is zero, because the 3

• vectors are stored separately

• the 3 vectors of the CSR my_mat structure: `MY_MAT_PTR`, `MY_MAT_IND`

• and `MY_MAT_DATA`

```mm.status()
```

Out:

```APDLMATH PARAMETER STATUS-  (      6 PARAMETERS DEFINED)

Name                   Type            Mem. (MB)       Dims            Workspace

IGMYHY                SMAT            0.011           [126:126]               1
MUWMJP                SMAT            0.011           [126:126]               1
MY_MAT                SMAT            0.000           [126:126]               1
MY_MAT_DATA           VEC             0.006           738             1
MY_MAT_IND            VEC             0.001           127             1
MY_MAT_PTR            VEC             0.003           738             1
```

## MAPDL Python Matrix Correspondence#

To determine which MAPDL object corresponds to which Python object, access the id property of the Python object.

```print("name(k)=" + k.id)
print("name(my_mat)=" + my_mat.id)
print("name(msub)=" + msub.id)
```

Out:

```name(k)=MUWMJP
name(my_mat)=my_mat
name(msub)=IGMYHY
```

stop mapdl

```mapdl.exit()
```

Total running time of the script: ( 0 minutes 0.703 seconds)

Gallery generated by Sphinx-Gallery
{"ft_lang_label":"__label__en","ft_lang_prob":0.560476,"math_prob":0.8950196,"size":2508,"snap":"2022-27-2022-33","text_gpt3_token_len":770,"char_repetition_ratio":0.1301917,"word_repetition_ratio":0.010526316,"special_character_ratio":0.3141946,"punctuation_ratio":0.15851273,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9978229,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-25T17:28:27Z\",\"WARC-Record-ID\":\"<urn:uuid:83a99315-04a8-4052-b74c-e0bb740540ca>\",\"Content-Length\":\"33637\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f679ce2e-7a2c-4535-8787-bde3d8a99670>\",\"WARC-Concurrent-To\":\"<urn:uuid:422df545-e9a9-4019-aab9-c197632a9762>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://mapdldocs.pyansys.com/examples/gallery_examples/01-apdlmath-examples/scipy_sparse_matrix.html\",\"WARC-Payload-Digest\":\"sha1:XK4BXM4NK7PGLF7E7TCTSHE5LC2T6LJ3\",\"WARC-Block-Digest\":\"sha1:65QK57GDUKRNF3XM7SYMWIUA23BBH6WN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103036077.8_warc_CC-MAIN-20220625160220-20220625190220-00067.warc.gz\"}"}