a.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = "Christian Heider Nielsen"
  4. __doc__ = r"""
  5. Created on 14-02-2021
  6. """
  7. import matlab
  8. import numpy
  9. from draugr.matlab_utilities import dict_to_sparse, matlab_to_ndarray, ndarray_to_matlab
  10. def asiduj_test():
  11. """
  12. Test of the module
  13. It runs the doctest and create other tests with matlab engine calls."""
  14. import scipy.linalg as spl
  15. print("Run matlab engine...")
  16. if len(matlab.engine.find_matlab()) == 0:
  17. # si aucune session share, run
  18. eng = matlab.engine.start_matlab()
  19. else:
  20. # connect to a session
  21. eng = matlab.engine.connect_matlab(matlab.engine.find_matlab()[0])
  22. print("connected...")
  23. print("Further tests....\n")
  24. # create matlab data
  25. # ------------------------------------------------------------------------
  26. mf = eng.rand(3)
  27. mc = matlab.double([[1 + 1j, 0.3, 1j], [1.2j - 1, 0, 1 + 1j]], is_complex=True)
  28. mi64 = matlab.int64([1, 2, 3])
  29. mi8 = matlab.int8([1, 2, 3])
  30. mb = matlab.logical([True, True, False])
  31. # Test conversion from matlab to numpy
  32. # ------------------------------------------------------------------------
  33. npf = matlab_to_ndarray(mf) # no copy, if mf is changed, npf change!
  34. npc = matlab_to_ndarray(mc) # still copy for complex (only)
  35. npi64 = matlab_to_ndarray(mi64)
  36. npi8 = matlab_to_ndarray(mi8)
  37. npb = matlab_to_ndarray(mb)
  38. # Test conversion from numpy to matlab
  39. # ------------------------------------------------------------------------
  40. npi = numpy.array(
  41. [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], dtype=numpy.int64, order="F"
  42. )
  43. mi = ndarray_to_matlab(npi)
  44. mc2 = ndarray_to_matlab(npc)
  45. mf2 = ndarray_to_matlab(npf) # copy, because npf has 'F' order (comes from mlarray)
  46. mi64_2 = ndarray_to_matlab(npi)
  47. mb2 = ndarray_to_matlab(npb)
  48. # test orientation in the matlab workspace
  49. # ------------------------------------------------------------------------
  50. eng.workspace["mi"] = mi64_2
  51. eng.workspace["mc2"] = mc2
  52. # check results
  53. # ------------------------------------------------------------------------
  54. npcc = numpy.array(
  55. [
  56. [1.0, 1.1 + 1j],
  57. [
  58. 1.12 + 0.13j,
  59. 22.1,
  60. ],
  61. ],
  62. dtype=numpy.complex,
  63. ) # assume C
  64. mcc = ndarray_to_matlab(npcc)
  65. npcc_inv = spl.inv(npcc)
  66. mcc_inv = eng.inv(mcc)
  67. print("Are the inverse of matrix equal ?")
  68. print(mcc_inv)
  69. print(npcc_inv)
  70. # # no copy check
  71. # # ------------------------------------------------------------------------
  72. # # complex
  73. #
  74. # npcc[0,0]=0.25
  75. # print("Are the data reuse ?", ", OWNDATA =", mcc._real.flags.owndata,
  76. # "same base =", mcc._real.base is npcc,
  77. # ', If one is modified, the other is modified =', mcc._real[0]==npcc[0,0])
  78. #
  79. # test sparse matrix requiert Recast4py.m
  80. K1, K2 = eng.sptest(3.0, nargout=2)
  81. Ksp1 = dict_to_sparse(K1)
  82. Ksp2 = dict_to_sparse(K2)
  83. def doctest_test():
  84. """run test procedure with doctest"""
  85. import doctest
  86. # invoke the testmod function to run tests contained in docstring
  87. stats = doctest.testmod()
  88. print(stats)
  89. return stats