|
6 | 6 | "github.com/MatProGo-dev/SymbolicMath.go/smErrors"
|
7 | 7 | "github.com/MatProGo-dev/SymbolicMath.go/symbolic"
|
8 | 8 | "gonum.org/v1/gonum/mat"
|
| 9 | + "math" |
9 | 10 | "strings"
|
10 | 11 | "testing"
|
11 | 12 | )
|
@@ -1721,3 +1722,145 @@ func TestMonomialVector_Derivative4(t *testing.T) {
|
1721 | 1722 | }
|
1722 | 1723 |
|
1723 | 1724 | }
|
| 1725 | + |
| 1726 | +/* |
| 1727 | +TestMonomialVector_Degree1 |
| 1728 | +Description: |
| 1729 | +
|
| 1730 | + Verifies that the Degree() method panics when this is called with a MonomialVector that is not well-defined. |
| 1731 | +*/ |
| 1732 | +func TestMonomialVector_Degree1(t *testing.T) { |
| 1733 | + // Constants |
| 1734 | + mv := symbolic.MonomialVector{} |
| 1735 | + |
| 1736 | + // Test |
| 1737 | + defer func() { |
| 1738 | + r := recover() |
| 1739 | + if r == nil { |
| 1740 | + t.Errorf( |
| 1741 | + "Expected mv.Degree() to panic; received %v", |
| 1742 | + mv.Degree(), |
| 1743 | + ) |
| 1744 | + } |
| 1745 | + |
| 1746 | + err := r.(error) |
| 1747 | + expectedError := mv.Check() |
| 1748 | + if err.Error() != expectedError.Error() { |
| 1749 | + t.Errorf( |
| 1750 | + "Expected error to be %v; received %v", |
| 1751 | + expectedError, |
| 1752 | + err, |
| 1753 | + ) |
| 1754 | + } |
| 1755 | + |
| 1756 | + }() |
| 1757 | + |
| 1758 | + mv.Degree() |
| 1759 | + t.Errorf("Test should panic before this is reached!") |
| 1760 | +} |
| 1761 | + |
| 1762 | +/* |
| 1763 | +TestMonomialVector_Degree2 |
| 1764 | +Description: |
| 1765 | +
|
| 1766 | + Verifies that the Degree() method returns the correct value when called with a well-defined MonomialVector. |
| 1767 | + This should be the maximum of all of the monomials inside the vector. |
| 1768 | +*/ |
| 1769 | +func TestMonomialVector_Degree2(t *testing.T) { |
| 1770 | + // Constants |
| 1771 | + v1 := symbolic.NewVariable() |
| 1772 | + v2 := symbolic.NewVariable() |
| 1773 | + m1 := symbolic.Monomial{ |
| 1774 | + Coefficient: 3.14, |
| 1775 | + VariableFactors: []symbolic.Variable{v1}, |
| 1776 | + Exponents: []int{2}, |
| 1777 | + } |
| 1778 | + m2 := symbolic.Monomial{ |
| 1779 | + Coefficient: 3.14, |
| 1780 | + VariableFactors: []symbolic.Variable{v2}, |
| 1781 | + Exponents: []int{1}, |
| 1782 | + } |
| 1783 | + mv := symbolic.MonomialVector{m1, m2} |
| 1784 | + |
| 1785 | + // Test |
| 1786 | + degree := mv.Degree() |
| 1787 | + |
| 1788 | + // Verify that the degree is correct |
| 1789 | + if degree != 2 { |
| 1790 | + t.Errorf( |
| 1791 | + "expected degree to be 2; received %v", |
| 1792 | + degree, |
| 1793 | + ) |
| 1794 | + } |
| 1795 | +} |
| 1796 | + |
| 1797 | +/* |
| 1798 | +TestMonomialVector_Power1 |
| 1799 | +Description: |
| 1800 | +
|
| 1801 | + Verifies that the Power() method panics when called with a vector that has Len() greater |
| 1802 | + than 1. |
| 1803 | +*/ |
| 1804 | +func TestMonomialVector_Power1(t *testing.T) { |
| 1805 | + // Constants |
| 1806 | + mv := symbolic.NewVariableVector(10).ToMonomialVector() |
| 1807 | + |
| 1808 | + // Test |
| 1809 | + defer func() { |
| 1810 | + r := recover() |
| 1811 | + if r == nil { |
| 1812 | + t.Errorf( |
| 1813 | + "Expected mv.Power(3) to panic; received %v", |
| 1814 | + mv.Power(3), |
| 1815 | + ) |
| 1816 | + } |
| 1817 | + }() |
| 1818 | + |
| 1819 | + mv.Power(3) |
| 1820 | + t.Errorf("Test should panic before this is reached!") |
| 1821 | +} |
| 1822 | + |
| 1823 | +/* |
| 1824 | +TestMonomialVector_Power2 |
| 1825 | +Description: |
| 1826 | +
|
| 1827 | + Verifies that the Power() method returns the correct value when called with a vector |
| 1828 | + of Len() == 1. The result should be a monomial vector where each monomial is raised to |
| 1829 | + the power of the input integer. |
| 1830 | +*/ |
| 1831 | +func TestMonomialVector_Power2(t *testing.T) { |
| 1832 | + // Constants |
| 1833 | + v1 := symbolic.NewVariable() |
| 1834 | + m1 := symbolic.Monomial{ |
| 1835 | + Coefficient: 3.14, |
| 1836 | + VariableFactors: []symbolic.Variable{v1}, |
| 1837 | + Exponents: []int{1}, |
| 1838 | + } |
| 1839 | + mv := symbolic.MonomialVector{m1} |
| 1840 | + |
| 1841 | + // Test |
| 1842 | + power := mv.Power(3) |
| 1843 | + |
| 1844 | + // Verify that the power is a monomial vector |
| 1845 | + if _, tf := power.(symbolic.Monomial); !tf { |
| 1846 | + t.Errorf( |
| 1847 | + "expected power to be a MonomialVector; received %T", |
| 1848 | + power, |
| 1849 | + ) |
| 1850 | + } |
| 1851 | + |
| 1852 | + // Verify that each monomial is raised to the power of 3 |
| 1853 | + if power.(symbolic.Monomial).Exponents[0] != 3 { |
| 1854 | + t.Errorf( |
| 1855 | + "expected monomial.Exponents[0] to be 3; received %v", |
| 1856 | + power.(symbolic.Monomial).Exponents[0], |
| 1857 | + ) |
| 1858 | + } |
| 1859 | + |
| 1860 | + if power.(symbolic.Monomial).Coefficient != math.Pow(3.14, 3) { |
| 1861 | + t.Errorf( |
| 1862 | + "expected monomial.Coefficient to be 3.14^3; received %v", |
| 1863 | + power.(symbolic.Monomial).Coefficient, |
| 1864 | + ) |
| 1865 | + } |
| 1866 | +} |
0 commit comments