.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/gallery_examples/01-apdlmath-examples/basic_operations.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_basic_operations.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_gallery_examples_01-apdlmath-examples_basic_operations.py:


.. _ref_mapdl_math_basic:

PyMAPDL APDLMath Basic Operations
---------------------------------

This tutorial shows how you can use pymapdl to use APDL math for basic
operations on APDLMath vectors and matrices in the APDL memory
workspace.

The `ansys.mapdl.math` submodule gives access to APDLMath features
inside PyMAPDL.

.. GENERATED FROM PYTHON SOURCE LINES 15-24

.. code-block:: default

    import numpy as np

    from ansys.mapdl.core import launch_mapdl

    # Start MAPDL as a service and create an APDLMath object.
    mapdl = launch_mapdl()
    mm = mapdl.math









.. GENERATED FROM PYTHON SOURCE LINES 25-35

Create and Manipulate Vectors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Create 2 APDLMath vectors of size 5. :math:`\vec{v}` is initialized with
ones, $\vec{w}$ is filled with random values

Corresponding APDLMath commands
- `*VEC,V,D,ALLOC,5`
- `*INIT,V,CONST,1`
- `*VEC,W,D,ALLOC,5`
- `*INIT,W,RAND`

.. GENERATED FROM PYTHON SOURCE LINES 35-41

.. code-block:: default


    v = mm.ones(5)
    w = mm.rand(5)
    print(w)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    IHSRMR :
     Size : 5
      4.170e-01   9.972e-01   7.203e-01   9.326e-01   1.144e-04      <       5




.. GENERATED FROM PYTHON SOURCE LINES 42-57

Use operators on vectors
~~~~~~~~~~~~~~~~~~~~~~~~
Just like `numpy` PyMAPDL APDLMath vectors can be have most of the
standard operators (e.g. ``+, -, +=, -=, *=``)

Here we form :math:`\vec{z}=\vec{v}+\vec{w}`

Then we compute :math:`\|z\|_2` (the default `norm` is nrm2, but you
can use `.norm('nrm1')` or `.norm('nrminf')` for different normals.
See `help(z.norm)` for additional details.

APDLMath Commands:
- `*VEC,Z,D,COPY,V`
- `*AXPY,1,,W,1,,Z`
- `*NRM,Z,,nrmval`

.. GENERATED FROM PYTHON SOURCE LINES 57-62

.. code-block:: default


    z = v + w
    z.norm()






.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    3.7001650749377593



.. GENERATED FROM PYTHON SOURCE LINES 63-78

Methods
~~~~~~~
Alternatively you can use methods, following the numpy
standards. Available methods are:

- `mm.add()`
- `mm.subtract()`
- `mm.dot()`

Equivalent operator:
`z = v + w`

Equivalent APDLMath Commands:
- `*VEC,Z,D,COPY,V`
- `*AXPY,1,,W,1,,Z`

.. GENERATED FROM PYTHON SOURCE LINES 78-81

.. code-block:: default

    z = mm.add(v, w)
    z.norm()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    3.7001650749377593



.. GENERATED FROM PYTHON SOURCE LINES 82-90

Subtraction

Equivalent operator:
z = v - w

Equivalent APDLMath Commands:
- `*VEC,Z,D,COPY,V`
- `*AXPY,-1,,W,1,,Z`

.. GENERATED FROM PYTHON SOURCE LINES 90-94

.. code-block:: default

    z = mm.subtract(v, w)
    print(z)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    LTGBHV :
     Size : 5
      5.830e-01   2.815e-03   2.797e-01   6.744e-02   9.999e-01      <       5




.. GENERATED FROM PYTHON SOURCE LINES 95-98

Dot product of 2 vectors

Equivalent APDLMath Command: `*DOT,V,W,dotval`

.. GENERATED FROM PYTHON SOURCE LINES 98-103

.. code-block:: default


    vw = mm.dot(v, w)
    print("Dot product :", str(vw))






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Dot product : 3.0672030387213454




.. GENERATED FROM PYTHON SOURCE LINES 104-112

Perform an in-place operations (without copying vectors)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In-Place Addition

MAPDL Commands:
- `*AXPY,1,,V,1,,Z`
- `*PRINT,Z`

.. GENERATED FROM PYTHON SOURCE LINES 112-116

.. code-block:: default

    v += v
    print(v)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    VLKKZY :
     Size : 5
      2.000e+00   2.000e+00   2.000e+00   2.000e+00   2.000e+00      <       5




.. GENERATED FROM PYTHON SOURCE LINES 117-120

In-Place Multiplication

MAPDL Command: `*SCAL,v,2`

.. GENERATED FROM PYTHON SOURCE LINES 120-123

.. code-block:: default

    v *= 2
    print(v)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    VLKKZY :
     Size : 5
      4.000e+00   4.000e+00   4.000e+00   4.000e+00   4.000e+00      <       5




.. GENERATED FROM PYTHON SOURCE LINES 124-126

In-Place Multiplication


.. GENERATED FROM PYTHON SOURCE LINES 126-130

.. code-block:: default

    v /= 2.0
    print(v)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    VLKKZY :
     Size : 5
      2.000e+00   2.000e+00   2.000e+00   2.000e+00   2.000e+00      <       5




.. GENERATED FROM PYTHON SOURCE LINES 131-141

Working with Dense Matrices
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allocate two dense matrices with random values.

MAPDL Commands:

- `*DMAT,m1,D,ALLOC,4,5`
- `*INIT,m1,RAND`
- `*DMAT,m1,D,ALLOC,4,5`
- `*INIT,m1,CONST,1`

.. GENERATED FROM PYTHON SOURCE LINES 141-146

.. code-block:: default


    m1 = mm.rand(4, 5)
    m2 = mm.ones(4, 5)
    m1, m2





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    (Dense APDLMath Matrix (4, 5), Dense APDLMath Matrix (4, 5))



.. GENERATED FROM PYTHON SOURCE LINES 147-152

**Add** these 2 dense matrices, and **scale** the result matrix.

Mapdl Commands
- `*DMAT,m3,D,COPY,m1`
- `*AXPY,1,,m2,1,,m3`

.. GENERATED FROM PYTHON SOURCE LINES 152-158

.. code-block:: default

    m3 = m1 + m2
    print(m3)

    m3 *= 2
    print(m3)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    MCGRKQ: 
     [1,1]: 1.417e+00 [1,2]: 1.000e+00 [1,3]: 1.147e+00 [1,4]: 1.186e+00 [1,5]: 1.397e+00 
     [2,1]: 1.997e+00 [2,2]: 1.128e+00 [2,3]: 1.236e+00 [2,4]: 1.388e+00 [2,5]: 1.936e+00 
     [3,1]: 1.720e+00 [3,2]: 1.302e+00 [3,3]: 1.092e+00 [3,4]: 1.346e+00 [3,5]: 1.539e+00 
     [4,1]: 1.933e+00 [4,2]: 1.999e+00 [4,3]: 1.397e+00 [4,4]: 1.670e+00 [4,5]: 1.846e+00
    MCGRKQ: 
     [1,1]: 2.834e+00 [1,2]: 2.000e+00 [1,3]: 2.294e+00 [1,4]: 2.373e+00 [1,5]: 2.794e+00 
     [2,1]: 3.994e+00 [2,2]: 2.256e+00 [2,3]: 2.472e+00 [2,4]: 2.776e+00 [2,5]: 3.871e+00 
     [3,1]: 3.441e+00 [3,2]: 2.605e+00 [3,3]: 2.185e+00 [3,4]: 2.691e+00 [3,5]: 3.078e+00 
     [4,1]: 3.865e+00 [4,2]: 3.998e+00 [4,3]: 2.793e+00 [4,4]: 3.339e+00 [4,5]: 3.693e+00




.. GENERATED FROM PYTHON SOURCE LINES 159-161

***Transpose*** a Matrix


.. GENERATED FROM PYTHON SOURCE LINES 161-165

.. code-block:: default

    m4 = m3.T
    print(m4)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    ZMOWWE: 
     [1,1]: 2.834e+00 [1,2]: 3.994e+00 [1,3]: 3.441e+00 [1,4]: 3.865e+00 
     [2,1]: 2.000e+00 [2,2]: 2.256e+00 [2,3]: 2.605e+00 [2,4]: 3.998e+00 
     [3,1]: 2.294e+00 [3,2]: 2.472e+00 [3,3]: 2.185e+00 [3,4]: 2.793e+00 
     [4,1]: 2.373e+00 [4,2]: 2.776e+00 [4,3]: 2.691e+00 [4,4]: 3.339e+00 
     [5,1]: 2.794e+00 [5,2]: 3.871e+00 [5,3]: 3.078e+00 [5,4]: 3.693e+00




.. GENERATED FROM PYTHON SOURCE LINES 166-167

As for vectors, methods are also available as an alternative to operators.

.. GENERATED FROM PYTHON SOURCE LINES 167-171

.. code-block:: default

    m3 = mm.add(m1, m2)
    print(m3)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    ISEWTZ: 
     [1,1]: 1.417e+00 [1,2]: 1.000e+00 [1,3]: 1.147e+00 [1,4]: 1.186e+00 [1,5]: 1.397e+00 
     [2,1]: 1.997e+00 [2,2]: 1.128e+00 [2,3]: 1.236e+00 [2,4]: 1.388e+00 [2,5]: 1.936e+00 
     [3,1]: 1.720e+00 [3,2]: 1.302e+00 [3,3]: 1.092e+00 [3,4]: 1.346e+00 [3,5]: 1.539e+00 
     [4,1]: 1.933e+00 [4,2]: 1.999e+00 [4,3]: 1.397e+00 [4,4]: 1.670e+00 [4,5]: 1.846e+00




.. GENERATED FROM PYTHON SOURCE LINES 172-174

Compute a matrix vector multiplication


.. GENERATED FROM PYTHON SOURCE LINES 174-178

.. code-block:: default

    mw = m3.dot(m4)
    print(mw)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    NIHTUA: 
     [1,1]: 1.536e+01 [1,2]: 1.945e+01 [1,3]: 1.748e+01 [1,4]: 2.180e+01 
     [2,1]: 1.945e+01 [2,2]: 2.492e+01 [2,3]: 2.220e+01 [2,4]: 2.746e+01 
     [3,1]: 1.748e+01 [3,2]: 2.220e+01 [3,3]: 2.005e+01 [3,4]: 2.508e+01 
     [4,1]: 2.180e+01 [4,2]: 2.746e+01 [4,3]: 2.508e+01 [4,4]: 3.176e+01




.. GENERATED FROM PYTHON SOURCE LINES 179-183

APDLMath matrices can be identified by printing, viewing their types, or with using the `__repr__` method by simply typing out the variable

APDLMath Matrix
~~~~~~~~~~~~~~~

.. GENERATED FROM PYTHON SOURCE LINES 183-188

.. code-block:: default

    type(m1)
    print(m1)
    m1






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    ZQNHIM: 
     [1,1]: 4.170e-01 [1,2]: 1.144e-04 [1,3]: 1.468e-01 [1,4]: 1.863e-01 [1,5]: 3.968e-01 
     [2,1]: 9.972e-01 [2,2]: 1.281e-01 [2,3]: 2.361e-01 [2,4]: 3.879e-01 [2,5]: 9.355e-01 
     [3,1]: 7.203e-01 [3,2]: 3.023e-01 [3,3]: 9.234e-02 [3,4]: 3.456e-01 [3,5]: 5.388e-01 
     [4,1]: 9.326e-01 [4,2]: 9.990e-01 [4,3]: 3.966e-01 [4,4]: 6.697e-01 [4,5]: 8.463e-01

    Dense APDLMath Matrix (4, 5)



.. GENERATED FROM PYTHON SOURCE LINES 189-191

APDLMath Vector


.. GENERATED FROM PYTHON SOURCE LINES 191-195

.. code-block:: default

    type(w)
    print(w)
    w





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    IHSRMR :
     Size : 5
      4.170e-01   9.972e-01   7.203e-01   9.326e-01   1.144e-04      <       5

    APDLMath Vector Size 5



.. GENERATED FROM PYTHON SOURCE LINES 196-201

Numpy methods on APDLMath objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Regardless of the underlying APDLMath object type, you are generally
able to perform most numpy or scipy operations on these arrays.  You
can do this one of two ways.  First, you can convert a matrix to a numpy array:

.. GENERATED FROM PYTHON SOURCE LINES 201-206

.. code-block:: default

    apdl_mat = mm.rand(5, 5)
    np_mat = apdl_mat.asarray()
    print(np_mat)






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    [[4.17021999e-01 1.28124448e-01 9.23385957e-02 6.69746040e-01
      4.19194519e-01]
     [9.97184808e-01 3.02332568e-01 3.96580726e-01 3.96767469e-01
      3.13273513e-01]
     [7.20324489e-01 9.99040516e-01 1.86260211e-01 9.35539073e-01
      6.85219501e-01]
     [9.32557361e-01 1.46755893e-01 3.87910740e-01 5.38816732e-01
      5.24548163e-01]
     [1.14381197e-04 2.36088976e-01 3.45560725e-01 8.46310918e-01
      2.04452249e-01]]




.. GENERATED FROM PYTHON SOURCE LINES 207-211

Alternatively, you can simply use numpy to compute the max of the array

This works because PyMAPDL copies over the matrix to the local
python memory and then computes the max using numpy.

.. GENERATED FROM PYTHON SOURCE LINES 211-214

.. code-block:: default

    print(np.max(apdl_mat))






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    0.9990405155112967




.. GENERATED FROM PYTHON SOURCE LINES 215-219

This works for most numpy operations, but keep in mind that
operations that are supported within MAPDL (such as adding or
multiplying arrays) will compute much faster as the data is not copied.


.. GENERATED FROM PYTHON SOURCE LINES 219-223

.. code-block:: default

    apdl_arr = mm.rand(5, 5)
    np_array = apdl_mat.asarray()
    print(np.allclose(apdl_mat, np_array))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    True




.. GENERATED FROM PYTHON SOURCE LINES 224-227

Stop mapdl
~~~~~~~~~~


.. GENERATED FROM PYTHON SOURCE LINES 227-228

.. code-block:: default

    mapdl.exit()








.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_basic_operations.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example




    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: basic_operations.py <basic_operations.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: basic_operations.ipynb <basic_operations.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_