-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1821 from lamorton/constants2
Adding @Constants
- Loading branch information
Showing
26 changed files
with
391 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import SymbolicUtils: symtype, term, hasmetadata, issym | ||
struct MTKConstantCtx end | ||
|
||
isconstant(x::Num) = isconstant(unwrap(x)) | ||
""" Test whether `x` is a constant-type Sym. """ | ||
function isconstant(x) | ||
x = unwrap(x) | ||
x isa Symbolic && getmetadata(x, MTKConstantCtx, false) | ||
end | ||
|
||
""" | ||
toconstant(s::Sym) | ||
Maps the parameter to a constant. The parameter must have a default. | ||
""" | ||
function toconstant(s::Sym) | ||
hasmetadata(s, Symbolics.VariableDefaultValue) || | ||
throw(ArgumentError("Constant `$(s)` must be assigned a default value.")) | ||
setmetadata(s, MTKConstantCtx, true) | ||
end | ||
|
||
toconstant(s::Num) = wrap(toconstant(value(s))) | ||
|
||
""" | ||
$(SIGNATURES) | ||
Define one or more constants. | ||
""" | ||
macro constants(xs...) | ||
Symbolics._parse_vars(:constants, | ||
Real, | ||
xs, | ||
toconstant) |> esc | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.