Skip to content

Commit

Permalink
Merge pull request #386 from JuliaSymbolics/myb/compat
Browse files Browse the repository at this point in the history
Update compat
  • Loading branch information
YingboMa authored Sep 21, 2021
2 parents 681551b + aa929f4 commit 0892bfd
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 78 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Symbolics"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
authors = ["Shashi Gowda <[email protected]>"]
version = "3.3.1"
version = "3.4.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down Expand Up @@ -46,7 +46,7 @@ SciMLBase = "1.8"
Setfield = "0.7"
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
StaticArrays = "1.1"
SymbolicUtils = "0.15.1"
SymbolicUtils = "0.16"
TreeViews = "0.3"
julia = "1.5"

Expand Down
2 changes: 1 addition & 1 deletion test/build_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ let # Symbolics.jl#123
@variables qd[1:6]
output_eq = u*(qd[1]*(M[1]*qd[1] + M[1]*qd[3] + M[1]*qd[4] + M[25]*qd[5] + M[31]*qd[6] + M[7]*qd[2]))

@test_reference "ctarget_functions/issue123.c" build_function(output_eq, x, target=Symbolics.CTarget())
@test_reference "target_functions/issue123.c" build_function(output_eq, x, target=Symbolics.CTarget())
end

using Symbolics: value
Expand Down
82 changes: 15 additions & 67 deletions test/build_targets.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
using Symbolics, Test
using ReferenceTests

@variables t a x(t) y(t)
expr = [a*x - x*y,-3y + x*y]
@test Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.StanTarget()) ==
"""
vector diffeqf(real t,vector internal_var___u,vector internal_var___p) {
vector[2] internal_var___du;
internal_var___du[1] = internal_var___p[1] * internal_var___u[1] + -1 * internal_var___u[1] * internal_var___u[2];
internal_var___du[2] = internal_var___u[1] * internal_var___u[2] + -3 * internal_var___u[2];
return internal_var___du;
}
"""
@test_reference "target_functions/1.stan" Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.StanTarget())

@test Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.CTarget(),
@test_reference "target_functions/1.c" Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.CTarget(),
lhsname=:internal_var___du,
rhsnames=[:internal_var___u,:internal_var___p,:t]) ==
"""
#include <math.h>
void diffeqf(double* internal_var___du, const double* internal_var___u, const double* internal_var___p, const double t) {
internal_var___du[0] = internal_var___p[0] * internal_var___u[0] + -1 * internal_var___u[0] * internal_var___u[1];
internal_var___du[1] = internal_var___u[0] * internal_var___u[1] + -3 * internal_var___u[1];
}
"""
rhsnames=[:internal_var___u,:internal_var___p,:t])

@test Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.MATLABTarget()) ==
"""
diffeqf = @(t,internal_var___u) [
internal_var___p(1) * internal_var___u(1) + -1 * internal_var___u(1) * internal_var___u(2);
internal_var___u(1) * internal_var___u(2) + -3 * internal_var___u(2);
];
"""
@test_reference "target_functions/1.m" Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.MATLABTarget())

@test Symbolics.build_function(expr,[x,y],[a],t,target = Symbolics.CTarget(),
lhsname=:internal_var___du,
Expand Down Expand Up @@ -64,29 +44,24 @@ let
expression = hcat(x,y,z) # Results in [x1 y1 z1; x2 y2 z2; ...]
variables = vcat(x,y,z) # Results in [x1, x2, x3, x4, y1, ...]
cfunc = build_function(expression, variables; target = Symbolics.CTarget(), expression = Val{true})

# Generated function should be out[0] = in[0], out[1] = in[1], out[2] = in[2], etc.
@test cfunc == "#include <math.h>\nvoid diffeqf(double* du, const double* RHS1) {\n du[0] = RHS1[0];\n du[1] = RHS1[1];\n du[2] = RHS1[2];\n du[3] = RHS1[3];\n du[4] = RHS1[4];\n du[5] = RHS1[5];\n du[6] = RHS1[6];\n du[7] = RHS1[7];\n du[8] = RHS1[8];\n du[9] = RHS1[9];\n du[10] = RHS1[10];\n du[11] = RHS1[11];\n}\n"
@test_reference "target_functions/matrix.c" cfunc
end

# Scalar CTarget test
let
@variables x y t
expression = x + y + t
cfunc = build_function(expression, [x], [y], t; target = Symbolics.CTarget(), expression = Val{true})

# Generated function should be out[0] = in1[0] + in2[0] + in3
@test cfunc == "#include <math.h>\nvoid diffeqf(double* du, const double* RHS1, const double* RHS2, const double RHS3) {\n du[0] = RHS3 + RHS1[0] + RHS2[0];\n}\n"
@test_reference "target_functions/scalar1.c" cfunc
end

# Scalar CTarget test with scalar multiplication and powers
let
@variables x y a t
expression = x^2 + y^-1 + sin(a)^3.5 + 2t
cfunc = build_function(expression, [x, y], [a], t; target = Symbolics.CTarget(), expression = Val{true})
@variables x y a t
expression = x^2 + y^-1 + sin(a)^3.5 + 2t
cfunc = build_function(expression, [x, y], [a], t; target = Symbolics.CTarget(), expression = Val{true})

# Generated function should avoid scalar multiplication of the form "4t" (currently done by adding another "* 1") and other invalid C syntax
@test cfunc == "#include <math.h>\nvoid diffeqf(double* du, const double* RHS1, const double* RHS2, const double RHS3) {\n du[0] = 2 * RHS3 * 1 + pow(RHS1[0], 2) + 1 / RHS1[1] + pow(sin(RHS2[0]), 3.5);\n}\n"
@test_reference "target_functions/scalar2.c" cfunc
end


Expand All @@ -97,25 +72,7 @@ let
variables = vcat(x,y,z) # Results in [x1, x2, x3, x4, y1, ...]
sfunc = build_function(expression, vcat(x,y), z, []; target = Symbolics.StanTarget(), expression = Val{true})

# Generated function should be out[1] = v[1], out[2] = v[2], ..., out[9] = p[9], etc.
@test sfunc == """
vector diffeqf(real Any[],vector internal_var___u,vector internal_var___p) {
vector[12] internal_var___du;
internal_var___du[1] = internal_var___u[1];
internal_var___du[2] = internal_var___u[2];
internal_var___du[3] = internal_var___u[3];
internal_var___du[4] = internal_var___u[4];
internal_var___du[5] = internal_var___u[5];
internal_var___du[6] = internal_var___u[6];
internal_var___du[7] = internal_var___u[7];
internal_var___du[8] = internal_var___u[8];
internal_var___du[9] = internal_var___p[1];
internal_var___du[10] = internal_var___p[2];
internal_var___du[11] = internal_var___p[3];
internal_var___du[12] = internal_var___p[4];
return internal_var___du;
}
"""
@test_reference "target_functions/matrix.stan" sfunc
end

# Scalar StanTarget test
Expand All @@ -124,14 +81,7 @@ let
expression = x + y + z
sfunc = build_function(expression, vcat(x,y), [z], t; target = Symbolics.StanTarget(), expression = Val{true})

# Generated function should be out[0] = v[0] + p[0] + t[0]
@test sfunc == """
vector diffeqf(real t,vector internal_var___u,vector internal_var___p) {
vector[1] internal_var___du;
internal_var___du[1] = internal_var___u[1] + internal_var___u[2] + internal_var___p[1];
return internal_var___du;
}
"""
@test_reference "target_functions/scalar1.stan" sfunc
end

# Matrix MATLABTarget test
Expand All @@ -141,8 +91,7 @@ let
variables = vcat(x,y,z) # Results in [x1, x2, x3, x4, y1, ...]
mfunc = build_function(expression, vcat(x,y,z); target = Symbolics.MATLABTarget(), expression = Val{true})

# Generated function should be of the same form as expression
@test mfunc == "diffeqf = @(t,internal_var___u) [\n internal_var___u(1), internal_var___u(5), internal_var___u(9);\n internal_var___u(2), internal_var___u(6), internal_var___u(10);\n internal_var___u(3), internal_var___u(7), internal_var___u(11);\n internal_var___u(4), internal_var___u(8), internal_var___u(12);\n];\n"
@test_reference "target_functions/matrix.m" mfunc
end

# Scalar MATLABTarget test
Expand All @@ -151,6 +100,5 @@ let
expression = x + y + z
mfunc = build_function(expression, vcat(x,y,z); target = Symbolics.MATLABTarget(), expression = Val{true})

# Generated function should be out[0] = v[0] + p[0] + t[0]
@test mfunc == "diffeqf = @(t,internal_var___u) [\n internal_var___u(1) + internal_var___u(2) + internal_var___u(3);\n];\n"
@test_reference "target_functions/scalar1.m" mfunc
end
4 changes: 0 additions & 4 deletions test/ctarget_functions/issue123.c

This file was deleted.

1 change: 0 additions & 1 deletion test/latexify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Dy = Differential(y)
@test_reference "latexify_refs/equation_vec1.txt" latexify([
x ~ y + z
y ~ x - 3z
z ~ -5x + 4y
])
@test_reference "latexify_refs/equation_vec2.txt" latexify([
Dx(u) ~ z
Expand Down
3 changes: 1 addition & 2 deletions test/latexify_refs/equation_vec1.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
\begin{align}
x =& y + z \\
y =& x - 3 z \\
z =& - 5 x + 4 y
y =& x - 3 z
\end{align}
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if GROUP == "All" || GROUP == "Core"
@safetestset "Fuzz Arrays" begin include("fuzz-arrays.jl") end
@safetestset "Differentiation Test" begin include("diff.jl") end
@safetestset "Difference Test" begin include("difference.jl") end
@safetestset "Degree Test" begin include("degree.jl") end
@safetestset "Degree Test" begin include("degree.jl") end
@safetestset "Is Linear or Affine Test" begin include("islinear_affine.jl") end
@safetestset "Linear Solver Test" begin include("linear_solver.jl") end
@safetestset "Overloading Test" begin include("overloads.jl") end
Expand Down
5 changes: 5 additions & 0 deletions test/target_functions/1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <math.h>
void diffeqf(double* internal_var___du, const double* internal_var___u, const double* internal_var___p, const double t) {
internal_var___du[0] = internal_var___p[0] * internal_var___u[0] + -1 * internal_var___u[0] * internal_var___u[1];
internal_var___du[1] = -3 * internal_var___u[1] + internal_var___u[0] * internal_var___u[1];
}
4 changes: 4 additions & 0 deletions test/target_functions/1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diffeqf = @(t,internal_var___u) [
internal_var___p(1) * internal_var___u(1) + -1 * internal_var___u(1) * internal_var___u(2);
-3 * internal_var___u(2) + internal_var___u(1) * internal_var___u(2);
];
6 changes: 6 additions & 0 deletions test/target_functions/1.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vector diffeqf(real t,vector internal_var___u,vector internal_var___p) {
vector[2] internal_var___du;
internal_var___du[1] = internal_var___p[1] * internal_var___u[1] + -1 * internal_var___u[1] * internal_var___u[2];
internal_var___du[2] = -3 * internal_var___u[2] + internal_var___u[1] * internal_var___u[2];
return internal_var___du;
}
4 changes: 4 additions & 0 deletions test/target_functions/issue123.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <math.h>
void diffeqf(double* du, const double* RHS1) {
du[0] = u * (M[0] * qd[0] + M[6] * qd[1] + M[0] * qd[2] + M[0] * qd[3] + M[24] * qd[4] + M[30] * qd[5]) * qd[0];
}
15 changes: 15 additions & 0 deletions test/target_functions/matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <math.h>
void diffeqf(double* du, const double* RHS1) {
du[0] = RHS1[0];
du[1] = RHS1[1];
du[2] = RHS1[2];
du[3] = RHS1[3];
du[4] = RHS1[4];
du[5] = RHS1[5];
du[6] = RHS1[6];
du[7] = RHS1[7];
du[8] = RHS1[8];
du[9] = RHS1[9];
du[10] = RHS1[10];
du[11] = RHS1[11];
}
6 changes: 6 additions & 0 deletions test/target_functions/matrix.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
diffeqf = @(t,internal_var___u) [
internal_var___u(1), internal_var___u(5), internal_var___u(9);
internal_var___u(2), internal_var___u(6), internal_var___u(10);
internal_var___u(3), internal_var___u(7), internal_var___u(11);
internal_var___u(4), internal_var___u(8), internal_var___u(12);
];
16 changes: 16 additions & 0 deletions test/target_functions/matrix.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
vector diffeqf(real Any[],vector internal_var___u,vector internal_var___p) {
vector[12] internal_var___du;
internal_var___du[1] = internal_var___u[1];
internal_var___du[2] = internal_var___u[2];
internal_var___du[3] = internal_var___u[3];
internal_var___du[4] = internal_var___u[4];
internal_var___du[5] = internal_var___u[5];
internal_var___du[6] = internal_var___u[6];
internal_var___du[7] = internal_var___u[7];
internal_var___du[8] = internal_var___u[8];
internal_var___du[9] = internal_var___p[1];
internal_var___du[10] = internal_var___p[2];
internal_var___du[11] = internal_var___p[3];
internal_var___du[12] = internal_var___p[4];
return internal_var___du;
}
4 changes: 4 additions & 0 deletions test/target_functions/scalar1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <math.h>
void diffeqf(double* du, const double* RHS1, const double* RHS2, const double RHS3) {
du[0] = RHS3 + RHS1[0] + RHS2[0];
}
3 changes: 3 additions & 0 deletions test/target_functions/scalar1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diffeqf = @(t,internal_var___u) [
internal_var___u(1) + internal_var___u(2) + internal_var___u(3);
];
5 changes: 5 additions & 0 deletions test/target_functions/scalar1.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vector diffeqf(real t,vector internal_var___u,vector internal_var___p) {
vector[1] internal_var___du;
internal_var___du[1] = internal_var___u[1] + internal_var___u[2] + internal_var___p[1];
return internal_var___du;
}
4 changes: 4 additions & 0 deletions test/target_functions/scalar2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <math.h>
void diffeqf(double* du, const double* RHS1, const double* RHS2, const double RHS3) {
du[0] = 2 * RHS3 * 1 + pow(RHS1[0], 2) + 1 / RHS1[1] + pow(sin(RHS2[0]), 3.5);
}

2 comments on commit 0892bfd

@YingboMa
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/45294

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.4.0 -m "<description of version>" 0892bfd1faf0128b820c193363d3b46b82efbbac
git push origin v3.4.0

Please sign in to comment.