|
5 | 5 | getKVector "github.com/MatProGo-dev/SymbolicMath.go/get/KVector"
|
6 | 6 | "github.com/MatProGo-dev/SymbolicMath.go/smErrors"
|
7 | 7 | "github.com/MatProGo-dev/SymbolicMath.go/symbolic"
|
| 8 | + "gonum.org/v1/gonum/mat" |
8 | 9 | "strings"
|
9 | 10 | "testing"
|
10 | 11 | )
|
@@ -852,6 +853,225 @@ func TestMonomialVector_Plus12(t *testing.T) {
|
852 | 853 | }
|
853 | 854 | }
|
854 | 855 |
|
| 856 | +/* |
| 857 | +TestMonomialVector_Minus1 |
| 858 | +Description: |
| 859 | +
|
| 860 | + Verifies that the Minus() method throws a panic when an improperly |
| 861 | + initialized vector of monomials is given. |
| 862 | +*/ |
| 863 | +func TestMonomialVector_Minus1(t *testing.T) { |
| 864 | + // Constants |
| 865 | + mv := symbolic.MonomialVector{} |
| 866 | + |
| 867 | + // Test |
| 868 | + defer func() { |
| 869 | + r := recover() |
| 870 | + if r == nil { |
| 871 | + t.Errorf( |
| 872 | + "Expected mv.Minus(3.14) to panic; received %v", |
| 873 | + mv.Minus(3.14), |
| 874 | + ) |
| 875 | + } |
| 876 | + }() |
| 877 | + |
| 878 | + mv.Minus(3.14) |
| 879 | +} |
| 880 | + |
| 881 | +/* |
| 882 | +TestMonomialVector_Minus2 |
| 883 | +Description: |
| 884 | +
|
| 885 | + Verifies that the Minus() method throws a panic when a well-formed |
| 886 | + vector of monomials is subtracted from an improperly initialized expression |
| 887 | + (in this case a monomial matrix). |
| 888 | +*/ |
| 889 | +func TestMonomialVector_Minus2(t *testing.T) { |
| 890 | + // Constants |
| 891 | + mv := symbolic.MonomialVector{ |
| 892 | + symbolic.NewVariable().ToMonomial(), |
| 893 | + symbolic.NewVariable().ToMonomial(), |
| 894 | + } |
| 895 | + pm := symbolic.MonomialMatrix{} |
| 896 | + |
| 897 | + // Test |
| 898 | + defer func() { |
| 899 | + r := recover() |
| 900 | + if r == nil { |
| 901 | + t.Errorf( |
| 902 | + "Expected mv.Minus(pm) to panic; received %v", |
| 903 | + mv.Minus(pm), |
| 904 | + ) |
| 905 | + } |
| 906 | + }() |
| 907 | + |
| 908 | + mv.Minus(pm) |
| 909 | +} |
| 910 | + |
| 911 | +/* |
| 912 | +TestMonomialVector_Minus3 |
| 913 | +Description: |
| 914 | +
|
| 915 | + Verifies that the Minus() method throws a panic when a well-formed |
| 916 | + vector of monomials is subtracted from a well formed vector expression |
| 917 | + OF THE WRONG DIMENSIONs. |
| 918 | +*/ |
| 919 | +func TestMonomialVector_Minus3(t *testing.T) { |
| 920 | + // Constants |
| 921 | + mv := symbolic.MonomialVector{ |
| 922 | + symbolic.NewVariable().ToMonomial(), |
| 923 | + symbolic.NewVariable().ToMonomial(), |
| 924 | + } |
| 925 | + pm := symbolic.PolynomialMatrix{ |
| 926 | + { |
| 927 | + symbolic.NewVariable().ToPolynomial(), |
| 928 | + symbolic.NewVariable().ToPolynomial(), |
| 929 | + symbolic.NewVariable().ToPolynomial(), |
| 930 | + }, |
| 931 | + } |
| 932 | + |
| 933 | + // Test |
| 934 | + defer func() { |
| 935 | + r := recover() |
| 936 | + if r == nil { |
| 937 | + t.Errorf( |
| 938 | + "Expected mv.Minus(pm) to panic; received %v", |
| 939 | + mv.Minus(pm), |
| 940 | + ) |
| 941 | + } |
| 942 | + |
| 943 | + rAsE, tf := r.(error) |
| 944 | + if !tf { |
| 945 | + t.Errorf( |
| 946 | + "Expected mv.Minus(pm) to panic with an error; received %v", |
| 947 | + r, |
| 948 | + ) |
| 949 | + } |
| 950 | + |
| 951 | + if !strings.Contains( |
| 952 | + rAsE.Error(), |
| 953 | + smErrors.DimensionError{ |
| 954 | + Operation: "Minus", |
| 955 | + Arg1: mv, |
| 956 | + Arg2: pm, |
| 957 | + }.Error(), |
| 958 | + ) { |
| 959 | + t.Errorf( |
| 960 | + "Expected mv.Minus(pm) to panic with an error containing \"dimensions\"; received %v", |
| 961 | + rAsE.Error(), |
| 962 | + ) |
| 963 | + } |
| 964 | + }() |
| 965 | + |
| 966 | + mv.Minus(pm) |
| 967 | +} |
| 968 | + |
| 969 | +/* |
| 970 | +TestMonomialVector_Minus4 |
| 971 | +Description: |
| 972 | +
|
| 973 | + Verifies that the Minus() method returns the correct value when a |
| 974 | + well-formed vector of monomials is subtracted from a float64. |
| 975 | + The result should be a polynomial vector where each polynomial contains |
| 976 | + two monomials, one that is a constant and one that is a variable. |
| 977 | +*/ |
| 978 | +func TestMonomialVector_Minus4(t *testing.T) { |
| 979 | + // Constants |
| 980 | + v1 := symbolic.NewVariable() |
| 981 | + mv := symbolic.MonomialVector{v1.ToMonomial(), v1.ToMonomial()} |
| 982 | + f2 := 3.14 |
| 983 | + |
| 984 | + // Test |
| 985 | + difference := mv.Minus(f2) |
| 986 | + |
| 987 | + // Verify that the difference is a polynomial vector |
| 988 | + if _, tf := difference.(symbolic.PolynomialVector); !tf { |
| 989 | + t.Errorf( |
| 990 | + "expected difference to be a PolynomialVector; received %T", |
| 991 | + difference, |
| 992 | + ) |
| 993 | + } |
| 994 | + |
| 995 | + // Verify that each polynomial contains two monomials |
| 996 | + for _, polynomial := range difference.(symbolic.PolynomialVector) { |
| 997 | + if len(polynomial.Monomials) != 2 { |
| 998 | + t.Errorf( |
| 999 | + "expected len(polynomial.Monomials) to be 2; received %v", |
| 1000 | + len(polynomial.Monomials), |
| 1001 | + ) |
| 1002 | + } |
| 1003 | + |
| 1004 | + for _, monomial := range polynomial.Monomials { |
| 1005 | + if (!monomial.IsConstant()) && (!monomial.IsVariable(v1)) { |
| 1006 | + t.Errorf("expected monomial to be a variable or a constant; received %v", monomial) |
| 1007 | + } |
| 1008 | + } |
| 1009 | + |
| 1010 | + } |
| 1011 | +} |
| 1012 | + |
| 1013 | +/* |
| 1014 | +TestMonomialVector_Minus5 |
| 1015 | +Description: |
| 1016 | +
|
| 1017 | + Verifies that the Minus() method returns the correct value when a |
| 1018 | + well-formed vector of monomials is subtracted from a *mat.VecDense object |
| 1019 | + of appropriate dimension. |
| 1020 | +*/ |
| 1021 | +func TestMonomialVector_Minus5(t *testing.T) { |
| 1022 | + // Setup |
| 1023 | + v1 := symbolic.NewVariable() |
| 1024 | + v2 := symbolic.NewVariable() |
| 1025 | + mv := symbolic.MonomialVector{v1.ToMonomial(), v2.ToMonomial()} |
| 1026 | + |
| 1027 | + // Create a *mat.VecDense object |
| 1028 | + vec := mat.NewVecDense(2, []float64{1, 2}) |
| 1029 | + |
| 1030 | + // Test |
| 1031 | + difference := mv.Minus(vec) |
| 1032 | + |
| 1033 | + // Verify that the difference is a polynomial vector |
| 1034 | + if _, tf := difference.(symbolic.PolynomialVector); !tf { |
| 1035 | + t.Errorf( |
| 1036 | + "expected difference to be a PolynomialVector; received %T", |
| 1037 | + difference, |
| 1038 | + ) |
| 1039 | + } |
| 1040 | + |
| 1041 | + // Verify that each polynomial contains two monomials |
| 1042 | + for ii, polynomial := range difference.(symbolic.PolynomialVector) { |
| 1043 | + if len(polynomial.Monomials) != 2 { |
| 1044 | + t.Errorf( |
| 1045 | + "expected len(polynomial.Monomials) to be 2; received %v", |
| 1046 | + len(polynomial.Monomials), |
| 1047 | + ) |
| 1048 | + } |
| 1049 | + |
| 1050 | + // Verify that each monomial is the correct value |
| 1051 | + for _, monomial := range polynomial.Monomials { |
| 1052 | + if monomial.IsConstant() { |
| 1053 | + switch ii { |
| 1054 | + case 0: |
| 1055 | + if monomial.Coefficient != -1.0 { |
| 1056 | + t.Errorf( |
| 1057 | + "expected monomial.Coefficient to be -1.0; received %v", |
| 1058 | + monomial.Coefficient, |
| 1059 | + ) |
| 1060 | + } |
| 1061 | + case 1: |
| 1062 | + if monomial.Coefficient != -2.0 { |
| 1063 | + t.Errorf( |
| 1064 | + "expected monomial.Coefficient to be -2.0; received %v", |
| 1065 | + monomial.Coefficient, |
| 1066 | + ) |
| 1067 | + } |
| 1068 | + } |
| 1069 | + |
| 1070 | + } |
| 1071 | + } |
| 1072 | + } |
| 1073 | +} |
| 1074 | + |
855 | 1075 | /*
|
856 | 1076 | TestMonomialVector_Multiply1
|
857 | 1077 | Description:
|
|
0 commit comments