.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/solve_dense_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_solve_dense_matrix.py: Use PyAnsys Math to solve a dense matrix linear system ------------------------------------------------------ This example shows how to use PyAnsys Math to solve a dense matrix linear system. .. GENERATED FROM PYTHON SOURCE LINES 29-43 .. code-block:: Python # Perform required imports and start PyAnsys # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Perform required imports. import time import matplotlib.pyplot as plt import numpy.linalg as npl import ansys.math.core.math as pymath # Start PyAnsys Math as a server. mm = pymath.AnsMath() .. GENERATED FROM PYTHON SOURCE LINES 44-47 Allocate dense matrix ~~~~~~~~~~~~~~~~~~~~~ Allocate a dense matrix in the MAPDL workspace. .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: Python mm._mapdl.clear() dim = 1000 a = mm.rand(dim, dim) b = mm.rand(dim) x = mm.zeros(dim) .. GENERATED FROM PYTHON SOURCE LINES 55-60 Copy matrices as NumPy arrays ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Copy the matrices as NumPy arrays before they are modified by a factorization call. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: Python a_py = a.asarray() b_py = b.asarray() .. GENERATED FROM PYTHON SOURCE LINES 64-68 Solve using PyAnsys Math ~~~~~~~~~~~~~~~~~~~~~~~~ Solve the dense matrix linear system using PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 68-77 .. code-block:: Python print(f"Solving a ({dim} x {dim}) dense linear system using PyAnsys Math...") t1 = time.time() s = mm.factorize(a) x = s.solve(b, x) t2 = time.time() pymath_time = t2 - t1 print(f"Elapsed time to solve the linear system using PyAnsys Math: {pymath_time} seconds") .. rst-class:: sphx-glr-script-out .. code-block:: none Solving a (1000 x 1000) dense linear system using PyAnsys Math... Elapsed time to solve the linear system using PyAnsys Math: 0.042484283447265625 seconds .. GENERATED FROM PYTHON SOURCE LINES 78-81 Get norm of solution ~~~~~~~~~~~~~~~~~~~~ Get the norm of the PyAnsys Math solution. .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python mm.norm(x) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.000000000000001 .. GENERATED FROM PYTHON SOURCE LINES 85-89 Solve using NumPy ~~~~~~~~~~~~~~~~~ Solve the dense matrix linear system using NumPy. .. GENERATED FROM PYTHON SOURCE LINES 89-97 .. code-block:: Python print(f"Solving a ({dim} x {dim}) dense linear system using NumPy...") t1 = time.time() x_py = npl.solve(a_py, b_py) t2 = time.time() numpy_time = t2 - t1 print(f"Elapsed time to solve the linear system using NumPy: {numpy_time} 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.024129390716552734 seconds .. GENERATED FROM PYTHON SOURCE LINES 98-103 Plot elapsed times ~~~~~~~~~~~~~~~~~~~ Plot the elapsed times for PyAnsys Math and Numpy to solve the dense matrix linear system. .. GENERATED FROM PYTHON SOURCE LINES 103-114 .. code-block:: Python max_time = max(pymath_time, numpy_time) fig = plt.figure(figsize=(12, 10)) ax = plt.axes() x = ["PyAnsys Math", "NumPy"] y = [pymath_time, numpy_time] plt.title("Elapsed time to solve the linear system") plt.ylim([0, max_time + 0.2 * max_time]) plt.ylabel("Elapsed time (s)") ax.bar(x, y, color="orange") plt.show() .. image-sg:: /examples/images/sphx_glr_solve_dense_matrix_001.png :alt: Elapsed time to solve the linear system :srcset: /examples/images/sphx_glr_solve_dense_matrix_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 115-118 Get norm of solution ~~~~~~~~~~~~~~~~~~~~ Get the norm of the NumPy solution. .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: Python npl.norm(x_py) .. rst-class:: sphx-glr-script-out .. code-block:: none np.float64(0.9999999999999996) .. GENERATED FROM PYTHON SOURCE LINES 122-125 Stop PyAnsys Math ~~~~~~~~~~~~~~~~~ Stop PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 125-127 .. code-block:: Python mm._mapdl.exit() .. rst-class:: sphx-glr-script-out .. code-block:: none /home/runner/work/pyansys-math/pyansys-math/.venv/lib/python3.10/site-packages/ansys/mapdl/core/launcher.py:818: 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.365 seconds) .. _sphx_glr_download_examples_solve_dense_matrix.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: solve_dense_matrix.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: solve_dense_matrix.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: solve_dense_matrix.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_