matrixContainerTest.py

You can view and download this file on Github: matrixContainerTest.py

 1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2# This is an EXUDYN example
 3#
 4# Details:  Simple test for MatrixContainer, similar as Examples given in Docu
 5#
 6# Author:   Johannes Gerstmayr
 7# Date:     2024-10-09
 8#
 9# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
10#
11#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
13import exudyn as exu
14
15useGraphics = True #without test
16#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
18try: #only if called from test suite
19    from modelUnitTests import exudynTestGlobals #for globally storing test results
20    useGraphics = exudynTestGlobals.useGraphics
21except:
22    class ExudynTestGlobals:
23        pass
24    exudynTestGlobals = ExudynTestGlobals()
25#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
27import numpy as np
28from scipy.sparse import csr_matrix
29from exudyn import MatrixContainer
30
31u = 0 #result
32mc = MatrixContainer()
33
34#Create MatrixContainer with dense matrix:
35#matrix can be initialized with a dense matrix, using a numpy array or list of lists , e.g.:
36matrix = np.eye(3)
37mcDense1 = MatrixContainer(matrix)
38u+= mcDense1.Convert2DenseMatrix()[0,1]
39mcDense2 = MatrixContainer([[1,2],[3,4]])
40
41u+= mcDense1.Convert2DenseMatrix()[1,0]
42mcSparse = MatrixContainer(csr_matrix(np.array([[1,5],[0,7]])))
43u+= mcSparse.Convert2DenseMatrix()[0,1]
44
45#Set with dense pyArray (a numpy array):
46pyArray = np.array(matrix)
47mc.SetWithDenseMatrix(pyArray, useDenseMatrix = True)
48u+= mc.Convert2DenseMatrix()[1,0]
49
50#Set empty matrix:
51mc.SetWithDenseMatrix([[]], useDenseMatrix = True)
52
53#Set with list of lists, stored as sparse matrix:
54mc.SetWithDenseMatrix([[1,2],[3,4]], useDenseMatrix = False)
55u+= mc.Convert2DenseMatrix()[1,0]
56
57#Set with sparse triplets (list of lists or numpy array):
58mc.SetWithSparseMatrix([[0,0,13.3],[1,1,4.2],[1,2,42.]],
59                       numberOfRows=2, numberOfColumns=3,
60                       useDenseMatrix=True)
61u+= mc.Convert2DenseMatrix()[1,2]
62
63exu.Print(mc)
64#gives dense matrix:
65#[[13.3  0.   0. ]
66# [ 0.   4.2 42. ]]
67
68#Set with scipy matrix:
69#WARNING: only use csr_matrix
70#         csc_matrix would basically run, but gives the transposed!!!
71spmat = csr_matrix(matrix)
72mc.SetWithSparseMatrix(spmat) #takes rows and column format automatically
73u+= mc.Convert2DenseMatrix()[1,1]
74
75#initialize and add triplets later on
76mc.Initialize(3,3,useDenseMatrix=False)
77mc.AddSparseMatrix(spmat, factor=1)
78#can also add smaller matrix
79mc.AddSparseMatrix(csr_matrix(np.eye(2)), factor=0.5)
80exu.Print('mc=',mc.Convert2DenseMatrix())
81u+= mc.Convert2DenseMatrix()[0,0]
82
83#set matrix with smaller sparse matrix than the final dimensions
84mc.SetWithSparseMatrix(csr_matrix(np.array([[1,2],[3,4]])), numberOfRows=4, numberOfColumns=2)
85exu.Print('mc=',mc.Convert2DenseMatrix())
86u+= mc.Convert2DenseMatrix()[1,1]
87
88
89result = u
90exu.Print('solution of matrixContainerTest=',result)
91
92exudynTestGlobals.testError = (result - (56.5))
93exudynTestGlobals.testResult = result