m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

6 months ago
  1. from sympy.core.expr import unchanged
  2. from sympy.core.symbol import Symbol, symbols
  3. from sympy.matrices.immutable import ImmutableDenseMatrix
  4. from sympy.matrices.expressions.companion import CompanionMatrix
  5. from sympy.polys.polytools import Poly
  6. from sympy.testing.pytest import raises
  7. def test_creation():
  8. x = Symbol('x')
  9. y = Symbol('y')
  10. raises(ValueError, lambda: CompanionMatrix(1))
  11. raises(ValueError, lambda: CompanionMatrix(Poly([1], x)))
  12. raises(ValueError, lambda: CompanionMatrix(Poly([2, 1], x)))
  13. raises(ValueError, lambda: CompanionMatrix(Poly(x*y, [x, y])))
  14. assert unchanged(CompanionMatrix, Poly([1, 2, 3], x))
  15. def test_shape():
  16. c0, c1, c2 = symbols('c0:3')
  17. x = Symbol('x')
  18. assert CompanionMatrix(Poly([1, c0], x)).shape == (1, 1)
  19. assert CompanionMatrix(Poly([1, c1, c0], x)).shape == (2, 2)
  20. assert CompanionMatrix(Poly([1, c2, c1, c0], x)).shape == (3, 3)
  21. def test_entry():
  22. c0, c1, c2 = symbols('c0:3')
  23. x = Symbol('x')
  24. A = CompanionMatrix(Poly([1, c2, c1, c0], x))
  25. assert A[0, 0] == 0
  26. assert A[1, 0] == 1
  27. assert A[1, 1] == 0
  28. assert A[2, 1] == 1
  29. assert A[0, 2] == -c0
  30. assert A[1, 2] == -c1
  31. assert A[2, 2] == -c2
  32. def test_as_explicit():
  33. c0, c1, c2 = symbols('c0:3')
  34. x = Symbol('x')
  35. assert CompanionMatrix(Poly([1, c0], x)).as_explicit() == \
  36. ImmutableDenseMatrix([-c0])
  37. assert CompanionMatrix(Poly([1, c1, c0], x)).as_explicit() == \
  38. ImmutableDenseMatrix([[0, -c0], [1, -c1]])
  39. assert CompanionMatrix(Poly([1, c2, c1, c0], x)).as_explicit() == \
  40. ImmutableDenseMatrix([[0, 0, -c0], [1, 0, -c1], [0, 1, -c2]])