Skip to content

Commit

Permalink
Add tests to handle Derivative when zero degree
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-adir committed Sep 9, 2023
1 parent 8317798 commit a4b5036
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 31 deletions.
12 changes: 3 additions & 9 deletions src/compmec/nurbs/calculus.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from fractions import Fraction
from typing import Any, Callable, Optional, Tuple
from typing import Any, Callable, Optional

import numpy as np

Expand Down Expand Up @@ -81,11 +81,6 @@ def nonrational_spline(curve: Curve) -> Curve:
assert isinstance(curve, Curve)
assert curve.weights is None
assert curve.degree + 1 != curve.npts
if curve.degree == 0:
newknotvector = curve.knotvector.limits
anypoint = curve.ctrlpoints[0]
newctrlpoints = [0 * anypoint]
return Curve(newknotvector, newctrlpoints)

knotvector = tuple(curve.knotvector)
matrix = heavy.Calculus.derivate_nonrational_spline(knotvector)
Expand Down Expand Up @@ -216,8 +211,7 @@ def density(
method: Optional[str] = None,
nnodes: Optional[int] = None,
) -> float:
"""Computes the integral :math:`I`
Computes the integral :math:`I`
"""Computes the integral I
The operation ``@`` is needed cause ``norm(curve(u)) = numpy.sqrt(curve(u) @ curve(u))``
Expand Down Expand Up @@ -290,7 +284,7 @@ def function(
nnodes: Optional[int] = None,
) -> float:
"""Computes the integral I
.. math::
I = \int_{a}^{b} g ( u ) \ du
Expand Down
15 changes: 0 additions & 15 deletions src/compmec/nurbs/heavy.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ def number_type(number: Union[int, float, Fraction]):
except TypeError:
if isinstance(number, (int, np.integer)):
return int
if isinstance(number, (float, np.floating)):
return float
if isinstance(number, Fraction):
return Fraction
return float
Expand Down Expand Up @@ -1235,19 +1233,6 @@ def add_spline_curve(
matrixb = Operations.matrix_transformation(knotvectorb, knotvectorc)
return totuple(matrixa), totuple(matrixb)

@staticmethod
def sub_spline_curve(
knotvectora: Tuple[float], knotvectorb: Tuple[float]
) -> Tuple["Matrix2D"]:
assert KnotVector.is_valid_vector(knotvectora)
assert KnotVector.is_valid_vector(knotvectorb)
assert knotvectora[0] == knotvectorb[0]
assert knotvectora[-1] == knotvectorb[-1]

matrixa, matrixb = MathOperations.add_spline_curve(knotvectora, knotvectorb)
matrixb = -np.array(matrixb, dtype="object")
return matrixa, totuple(matrixb)

@staticmethod
def mul_spline_curve(
knotvectora: Tuple[float], knotvectorb: Tuple[float]
Expand Down
12 changes: 7 additions & 5 deletions tests/test_calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_begin(self):
def test_bezier(self):
deltau = 1e-6
usample = np.linspace(2 * deltau, 1 - 2 * deltau, 5)
for degree in range(1, 4):
for degree in range(0, 4):
vector = GeneratorKnotVector.bezier(degree)
curve = Curve(vector)
points = np.random.uniform(-1, 1, curve.npts)
Expand All @@ -140,7 +140,7 @@ def test_bezier(self):
)
def test_spline(self):
deltau = 1e-6
for degree in range(1, 4):
for degree in range(0, 4):
for npts in range(degree + 3, degree + 9):
vector = GeneratorKnotVector.uniform(degree, npts)
knots = vector.knots
Expand Down Expand Up @@ -169,7 +169,7 @@ def test_spline(self):
def test_rationalbezier(self):
deltau = 1e-6
usample = np.linspace(2 * deltau, 1 - 2 * deltau, 5)
for degree in range(1, 4):
for degree in range(0, 4):
vector = GeneratorKnotVector.bezier(degree)
curve = Curve(vector)
points = np.random.uniform(-1, 1, curve.npts)
Expand All @@ -194,7 +194,7 @@ def test_rationalbezier(self):
)
def test_rationalspline(self):
deltau = 1e-6
for degree in range(1, 4):
for degree in range(0, 4):
for npts in range(degree + 3, degree + 9):
vector = GeneratorKnotVector.uniform(degree, npts)
knots = vector.knots
Expand Down Expand Up @@ -310,7 +310,9 @@ def test_lenght_integral(self):
curve.ctrlpoints = points

test = Integrate.lenght(curve)
good = sum(np.linalg.norm(points[i+1] - points[i]) for i in range(curve.npts-1))
good = sum(
np.linalg.norm(points[i + 1] - points[i]) for i in range(curve.npts - 1)
)

assert abs(test - good) < 1e-9

Expand Down
21 changes: 19 additions & 2 deletions tests/test_splinecurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,26 @@ def test_fractions(self):
assert type(tval) is frac
assert tval == gval

@pytest.mark.order(5)
@pytest.mark.timeout(10)
@pytest.mark.dependency(depends=["TestOthers::test_begin"])
def test_fraction_function(self):
knotvector = GeneratorKnotVector.uniform(3, 10)
curve = Curve(knotvector)
curve.ctrlpoints = np.random.uniform(-1, 1, curve.npts)

num, den = curve.fraction()
newcurve = num / den
assert curve == newcurve

@pytest.mark.order(5)
@pytest.mark.dependency(
depends=["TestOthers::test_begin", "TestOthers::test_others"]
depends=[
"TestOthers::test_begin",
"TestOthers::test_others",
"TestOthers::test_fractions",
"TestOthers::test_fraction_function",
]
)
def test_end(self):
pass
Expand All @@ -1009,7 +1026,7 @@ def test_end(self):
"TestSplitUnite::test_end",
"TestDegreeOperations::test_end",
"TestSumSubtract::test_end",
"TestOthers::test_others",
"TestOthers::test_end",
]
)
def test_end():
Expand Down

0 comments on commit a4b5036

Please sign in to comment.