Skip to content

Commit

Permalink
final fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xtalax committed Aug 7, 2023
1 parent dfc7d94 commit 73352b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ Laplacian(vars) = Nabla(vars) ⋅ Nabla(vars)

function (D::ArrayDifferentialOperator)(x::SymVec)
@assert length(D.vars) == length(x) "Vector must be same length as vars in Operator $(D.name)."
@arrayop (i,) unwrap((D.differentials)[i](x[i])) term=D(y) reduce=+
@arrayop (i,) (D.differentials)[i](x[i]) term=D(y)
end
(D::ArrayDifferentialOperator)(x::Arr) = Arr(D(value(x)))

Expand All @@ -821,9 +821,10 @@ end

function LinearAlgebra.dot(D::ArrayDifferentialOperator, x::SymVec)
@assert length(D.vars) == length(x) "Vector must be same length as vars in Operator $(D.name)."
sum(D(x))
@show D(x), scalarize(D(x))
sum(scalarize(D(x)))
end
LinearAlgebra.dot(D::ArrayDifferentialOperator, x::Arr) = D value(x)
LinearAlgebra.dot(D::ArrayDifferentialOperator, x::Arr) = Num(D value(x))

function LinearAlgebra.dot(x::SymVec, D::ArrayDifferentialOperator)
@assert length(D.vars) == length(x) "Vector must be same length as vars in Operator $(D.name)."
Expand All @@ -837,16 +838,6 @@ function LinearAlgebra.dot(D1::ArrayDifferentialOperator, D2::ArrayDifferentialO
(x) -> @arrayop (i,) lap(x[i]) term=(D1D2)(x) reduce=+
end

function εijk_cond(i, j, k)
if (i, j, k) in [(1, 2, 3), (2, 3, 1), (3, 1, 2)]
1
elseif (i == j) || (j == k) || (k == i)
0
else
-1
end
end

function crosscompose(a, b)
v1 = x -> (a[2] b[3])(x) - (a[3] b[2])(x)
v2 = x -> (a[3] b[1])(x) - (a[1] b[3])(x)
Expand Down Expand Up @@ -884,4 +875,4 @@ Base.nameof(D::ArrayDifferentialOperator) = Symbol(D.name)
function Base.:(==)(D1::ArrayDifferentialOperator, D2::ArrayDifferentialOperator)
@variables x[1:length(D1.vars)]
all(scalarize(isequal.(D1.vars, D2.vars))) && all(scalarize(isequal.(D1(x), D2(x))))
end
end
38 changes: 38 additions & 0 deletions test/diff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,41 @@ let
@test isequal(expand_derivatives(Differential(t)(im*t)), im)
@test isequal(expand_derivatives(Differential(t)(t^2 + im*t)), 2t + im)
end

## Vector Calculus


@variables x[1:3] y[1:3]

= Nabla(x)

out = (y)
@test all(isequal.(scalarize(out), [Differential(x[1])(y[1]), Differential(x[2])(y[2]), Differential(x[3])(y[3])]))


out = y
@test isequal(out, Div(x)(y))
@test isequal(out, Differential(x[1])(y[1]) + Differential(x[2])(y[2]) + Differential(x[3])(y[3]))

out =× y
@test isequal(out, Curl(x)(y))
@test all(isequal.(scalarize(out), [Differential(x[2])(y[3]) - Differential(x[3])(y[2]),
Differential(x[3])(y[1]) - Differential(x[1])(y[3]),
Differential(x[1])(y[2]) - Differential(x[2])(y[1])]))



out = (∇ ∇)(y)
@test isequal(out, Laplacian(x)(y))
@test all(isequal.(scalarize(out), [Differential(x[1])(Differential(x[1])(y[1])) + Differential(x[2])(Differential(x[2])(y[1])) + Differential(x[3])(Differential(x[3])(y[1])),
Differential(x[1])(Differential(x[1])(y[2])) + Differential(x[2])(Differential(x[2])(y[2])) + Differential(x[3])(Differential(x[3])(y[2])),
Differential(x[1])(Differential(x[1])(y[3])) + Differential(x[2])(Differential(x[2])(y[3])) + Differential(x[3])(Differential(x[3])(y[3]))]))


out = (∇ × ∇)(y)

@test isequal(out, Curl(x)(Curl(x)(y)))
@test all(isequal.(scalarize(out), [Differential(x[2])(Differential(x[3])(y[1])) - Differential(x[3])(Differential(x[2])(y[1])),
Differential(x[3])(Differential(x[1])(y[2])) - Differential(x[1])(Differential(x[3])(y[2])),
Differential(x[1])(Differential(x[2])(y[3])) - Differential(x[2])(Differential(x[1])(y[3]))]))

0 comments on commit 73352b9

Please sign in to comment.