Enforce constraints in the editor

This commit is contained in:
Aaron Fenyes 2024-10-26 23:51:27 -07:00
parent ce33bbf418
commit a37c71153d
3 changed files with 151 additions and 36 deletions

View file

@ -1,5 +1,6 @@
use lazy_static::lazy_static;
use nalgebra::{Const, DMatrix, DVector, Dyn};
use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */
// --- elements ---
@ -40,9 +41,30 @@ struct MatrixEntry {
value: f64
}
struct PartialMatrix(Vec<MatrixEntry>);
pub struct PartialMatrix(Vec<MatrixEntry>);
impl PartialMatrix {
pub fn new() -> PartialMatrix {
PartialMatrix(Vec::<MatrixEntry>::new())
}
pub fn push_sym(&mut self, row: usize, col: usize, value: f64) {
let PartialMatrix(entries) = self;
entries.push(MatrixEntry { index: (row, col), value: value });
if row != col {
entries.push(MatrixEntry { index: (col, row), value: value });
}
}
/* DEBUG */
pub fn log_to_console(&self) {
let PartialMatrix(entries) = self;
for ent in entries {
let ent_str = format!("{} {} {}", ent.index.0, ent.index.1, ent.value);
console::log_1(&JsValue::from(ent_str.as_str()));
}
}
fn proj(&self, a: &DMatrix<f64>) -> DMatrix<f64> {
let mut result = DMatrix::<f64>::zeros(a.nrows(), a.ncols());
let PartialMatrix(entries) = self;
@ -64,13 +86,13 @@ impl PartialMatrix {
// --- descent history ---
struct DescentHistory {
config: Vec<DMatrix<f64>>,
scaled_loss: Vec<f64>,
neg_grad: Vec<DMatrix<f64>>,
min_eigval: Vec<f64>,
base_step: Vec<DMatrix<f64>>,
backoff_steps: Vec<i32>
pub struct DescentHistory {
pub config: Vec<DMatrix<f64>>,
pub scaled_loss: Vec<f64>,
pub neg_grad: Vec<DMatrix<f64>>,
pub min_eigval: Vec<f64>,
pub base_step: Vec<DMatrix<f64>>,
pub backoff_steps: Vec<i32>
}
impl DescentHistory {
@ -148,7 +170,7 @@ fn seek_better_config(
// seek a matrix `config` for which `config' * Q * config` matches the partial
// matrix `gram`. use gradient descent starting from `guess`
fn realize_gram(
pub fn realize_gram(
gram: &PartialMatrix,
guess: DMatrix<f64>,
frozen: &[(usize, usize)],