.. 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/solve_dense_matrix.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_solve_dense_matrix.py>`
        to download the full example code

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

.. _sphx_glr_examples_gallery_examples_01-apdlmath-examples_solve_dense_matrix.py:


Use APDLMath to Solve a Dense Matrix Linear System
--------------------------------------------------

Use the APDLMath module to solve a Dense Matrix Linear System.

.. GENERATED FROM PYTHON SOURCE LINES 8-20

.. code-block:: default


    import time

    import numpy.linalg 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 21-23

Allocate a Dense Matrix in the APDLMath workspace


.. GENERATED FROM PYTHON SOURCE LINES 23-29

.. code-block:: default

    mapdl.clear()
    dim = 1000
    a = mm.rand(dim, dim)
    b = mm.rand(dim)
    x = mm.zeros(dim)








.. GENERATED FROM PYTHON SOURCE LINES 30-33

Copy the matrices as numpy arrays before they are modified by
factorization call


.. GENERATED FROM PYTHON SOURCE LINES 33-36

.. code-block:: default

    a_py = a.asarray()
    b_py = b.asarray()








.. GENERATED FROM PYTHON SOURCE LINES 37-39

Solve using APDLMath


.. GENERATED FROM PYTHON SOURCE LINES 39-47

.. code-block:: default

    print(f"Solving a ({dim} x {dim}) dense linear system using MAPDL...")

    t1 = time.time()
    s = mm.factorize(a)
    x = s.solve(b, x)
    t2 = time.time()
    print(f"Elapsed time to solve the linear system using Mapdl: {t2 - t1} seconds")





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

 .. code-block:: none

    Solving a (1000 x 1000) dense linear system using MAPDL...
    Elapsed time to solve the linear system using Mapdl: 0.014746665954589844 seconds




.. GENERATED FROM PYTHON SOURCE LINES 48-49

Norm of the MAPDL Solution

.. GENERATED FROM PYTHON SOURCE LINES 49-52

.. code-block:: default

    mm.norm(x)






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

 .. code-block:: none


    1.0000000000000016



.. GENERATED FROM PYTHON SOURCE LINES 53-55

Solve the solution using numpy


.. GENERATED FROM PYTHON SOURCE LINES 55-62

.. code-block:: default

    print(f"Solving a ({dim} x {dim}) dense linear system using numpy...")

    t1 = time.time()
    x_py = np.linalg.solve(a_py, b_py)
    t2 = time.time()
    print(f"Elapsed time to solve the linear system using numpy: {t2 - t1} seconds")





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

 .. code-block:: none

    Solving a (1000 x 1000) dense linear system using numpy...
    Elapsed time to solve the linear system using numpy: 0.08562207221984863 seconds




.. GENERATED FROM PYTHON SOURCE LINES 63-65

Norm of the numpy Solution


.. GENERATED FROM PYTHON SOURCE LINES 65-67

.. code-block:: default

    np.linalg.norm(x_py)





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

 .. code-block:: none


    0.9999999999999987



.. GENERATED FROM PYTHON SOURCE LINES 68-71

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


.. GENERATED FROM PYTHON SOURCE LINES 71-72

.. code-block:: default

    mapdl.exit()








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

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


.. _sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_solve_dense_matrix.py:

.. only:: html

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




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

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

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

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


.. only:: html

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

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