Skip to content

Commit

Permalink
Add fidget::solver module (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkeeter authored Jun 4, 2024
1 parent 67cd2b6 commit 629432b
Show file tree
Hide file tree
Showing 8 changed files with 674 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
type to match `Context::get_var`.
- Added `impl From<i32> for Tree` to make writing tree expressions easier
- Removed `Error::ReservedName` and `Error::DuplicateName`, which were unused
- Add the `fidget::solver` module, which contains a simple solver for systems of
equations. The solver requires the equations to implement Fidget's `Function`
trait. It uses both point-wise and gradient evaluation to solve for a set of
`Var` values, using the Levenberg-Marquardt algorithm.
- Add `Tree::var()` and `impl TryFrom<Tree> for Var`

# 0.3.0
- Major refactoring of core evaluation traits
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion fidget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ windows = { version = "0.54.0", features = ["Win32_Foundation", "Win32_System_Me
getrandom = { version = "0.2", features = ["js"] }

[features]
default = ["jit", "rhai", "render", "mesh"]
default = ["jit", "rhai", "render", "mesh", "solver"]

## Enables fast evaluation via a JIT compiler. This is exposed in the
## [`fidget::jit`](crate::jit) module, and is supported on macOS, Linux, and
Expand All @@ -60,13 +60,18 @@ render = []
## Enable 3D meshing, in the [`fidget::mesh`](crate::mesh) module
mesh = ["dep:crossbeam-deque"]

## Enable a system-of-equations solver, in the [`fidget::solver`](crate::solver)
## module
solver = []

## Enable `eval-tests` if you're writing your own evaluators and want to
## unit-test them. When enabled, the crate exports a set of macros to test each
## evaluator type, e.g. `float_slice_tests!(...)`.
eval-tests = []

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
approx = "0.5.1"

[[bench]]
name = "render"
Expand Down
18 changes: 17 additions & 1 deletion fidget/src/core/context/tree.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Context-free math trees
use super::op::{BinaryOpcode, UnaryOpcode};
use crate::var::Var;
use crate::{var::Var, Error};
use std::sync::Arc;

/// Opcode type for trees
Expand Down Expand Up @@ -159,6 +159,22 @@ impl Tree {
z: z.0,
}))
}

/// Returns the inner [`Var`] if this is an input tree, or `None`
pub fn var(&self) -> Option<Var> {
if let TreeOp::Input(v) = &*self.0 {
Some(*v)
} else {
None
}
}
}

impl TryFrom<Tree> for Var {
type Error = Error;
fn try_from(t: Tree) -> Result<Var, Error> {
t.var().ok_or(Error::NotAVar)
}
}

/// See [`Context`](crate::Context) for documentation of these functions
Expand Down
4 changes: 4 additions & 0 deletions fidget/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub enum Error {
#[error("variable index ({0}) exceeds max var index for this tape ({1})")]
BadVarIndex(usize, usize),

/// Could not solve for matrix pseudo-inverse
#[error("could not solve for matrix pseudo-inverse: {0}")]
SingularMatrix(&'static str),

/// io error; see inner code for details
#[error("io error: {0}")]
IoError(#[from] std::io::Error),
Expand Down
3 changes: 3 additions & 0 deletions fidget/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,6 @@ pub mod jit;

#[cfg(feature = "mesh")]
pub mod mesh;

#[cfg(feature = "solver")]
pub mod solver;
Loading

0 comments on commit 629432b

Please sign in to comment.