Skip to content

Commit 325bced

Browse files
authored
Implement grad of VectorFunction (#299)
* add py.typed * rename AnyFunction -> Function * integrate components of vector function * ruff * write test * DefElement branch * diff(VectorFunction) * ruff * no need for ._vec
1 parent 742a0f2 commit 325bced

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

.github/workflows/defelement.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
with:
2828
path: ./defelement
2929
repository: DefElement/DefElement
30-
ref: mscroggs/AnyFunction->Function
30+
ref: main
3131
- name: Install requirements
3232
run: |
3333
cd defelement

CHANGELOG_SINCE_LAST_VERSION.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
- Allow "gl" variant of Lagrange element
2+
- Implement component-wise integrals of VectorFunction and MatrixFunction
3+
- Implement grad of VectorFunction

symfem/functions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def div(self):
275275
"""
276276

277277
@abstractmethod
278-
def grad(self, dim: int):
278+
def grad(self, dim: int) -> Function:
279279
"""Compute the grad of the function.
280280
281281
Returns:
@@ -757,7 +757,7 @@ def div(self):
757757
"""
758758
raise ValueError("Cannot compute the div of a scalar-valued function.")
759759

760-
def grad(self, dim: int) -> VectorFunction:
760+
def grad(self, dim: int) -> Function:
761761
"""Compute the grad of the function.
762762
763763
Returns:
@@ -1215,13 +1215,13 @@ def div(self) -> ScalarFunction:
12151215
out += i.diff(j)
12161216
return out
12171217

1218-
def grad(self):
1218+
def grad(self, dim: int) -> Function:
12191219
"""Compute the grad of the function.
12201220
12211221
Returns:
12221222
The gradient
12231223
"""
1224-
raise ValueError("Cannot compute the grad of a vector-valued function.")
1224+
return MatrixFunction([f.grad(dim) for f in self._vec])
12251225

12261226
def curl(self) -> VectorFunction:
12271227
"""Compute the curl of the function.
@@ -1721,7 +1721,7 @@ def div(self):
17211721
"""
17221722
raise ValueError("Cannot compute the div of a matrix-valued function.")
17231723

1724-
def grad(self):
1724+
def grad(self, dim: int) -> Function:
17251725
"""Compute the grad of the function.
17261726
17271727
Returns:

symfem/piecewise_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def div(self) -> PiecewiseFunction:
413413
"""
414414
return PiecewiseFunction({shape: f.div() for shape, f in self._pieces.items()}, self.tdim)
415415

416-
def grad(self, dim: int) -> PiecewiseFunction:
416+
def grad(self, dim: int) -> Function:
417417
"""Compute the grad of the function.
418418
419419
Returns:

test/test_functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ def test_piecewise_scalar_function_subs():
192192
assert f2.subs(x[:2], (1, 1)) == 1
193193

194194

195+
def test_vector_grad():
196+
f = VectorFunction([x[0], x[1], x[1] ** 2 - 3 * x[2]])
197+
d = f.grad(3)
198+
199+
assert d[0, 0] == 1
200+
assert d[0, 1] == 0
201+
assert d[0, 2] == 0
202+
assert d[1, 0] == 0
203+
assert d[1, 1] == 1
204+
assert d[1, 2] == 0
205+
assert d[2, 0] == 0
206+
assert d[2, 1] == 2 * x[1]
207+
assert d[2, 2] == -3
208+
209+
195210
def test_vector_integral():
196211
f = VectorFunction([1, x[0], x[0] * x[1] ** 2])
197212
i = f.integral(create_reference("triangle"))

0 commit comments

Comments
 (0)