# Use APDLMath to Solve a Dense Matrix Linear System#

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

```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
```

Allocate a Dense Matrix in the APDLMath workspace

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

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

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

Solve using APDLMath

```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")
```
```Solving a (1000 x 1000) dense linear system using MAPDL...
Elapsed time to solve the linear system using Mapdl: 0.06417131423950195 seconds
```

Norm of the MAPDL Solution

```mm.norm(x)
```
```1.000000000000001
```

Solve the solution using numpy

```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")
```
```Solving a (1000 x 1000) dense linear system using numpy...
Elapsed time to solve the linear system using numpy: 0.03297615051269531 seconds
```

Norm of the numpy Solution

```np.linalg.norm(x_py)
```
```0.9999999999999996
```

stop mapdl

```mapdl.exit()
```

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

Gallery generated by Sphinx-Gallery