Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dezorr: reintroduce var #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 10 additions & 63 deletions dezorr/src/arrow.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,17 @@
#![allow(dead_code)]

use {
crate::{func::Function, types::ContinuousDomain},
std::{cell::RefCell, rc::Rc},
crate::{func::Function, types::ContinuousDomain, var::Variable},
std::rc::Rc,
};

#[macro_export]
macro_rules! DFN {
($f: expr) => {
Some(Box::new(|xs| {
xs.iter().map(|r| $f(r.clone())).collect::<Vec<_>>()
}))
};
}

#[macro_export]
macro_rules! TFN {
($f: expr) => {
Some(Box::new($f))
};
}

pub type ArrowType<D> = Box<dyn Fn(&[D]) -> Vec<D>>;

#[derive(Clone)]
struct ConnectionBody<'a, D: ContinuousDomain> {
value: Option<D>,
source: &'a Function<'a, D>,
target: &'a Function<'a, D>,
}

#[derive(Clone)]
pub struct Connection<'a, D: ContinuousDomain>(Rc<RefCell<ConnectionBody<'a, D>>>);

impl<'a, D: ContinuousDomain + std::fmt::Debug> std::fmt::Debug for Connection<'a, D> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let binding = self.0.borrow();
f.debug_struct("Connection")
.field("vaule", &binding.value)
.finish()
}
}

impl<'a, D: ContinuousDomain> Connection<'a, D> {
pub fn new(value: Option<D>, source: &'a Function<'a, D>, target: &'a Function<'a, D>) -> Self {
Connection(Rc::new(RefCell::new(ConnectionBody {
value,
source,
target,
})))
}
pub fn get_value(&self) -> Option<D> {
self.0.borrow().value.clone()
}
pub fn set_value(&self, val: Option<D>) {
self.0.borrow_mut().value = val;
}
}

#[allow(clippy::complexity)]
#[derive(Default)]
pub struct Arrow<'a, D: ContinuousDomain> {
domain: Vec<Connection<'a, D>>,
domain: Vec<Variable<'a, D>>,
pub arrow: Option<Rc<ArrowType<D>>>,
values: Vec<D>,
codomain: Vec<Connection<'a, D>>,
codomain: Vec<Variable<'a, D>>,
}

impl<D: ContinuousDomain> Clone for Arrow<'_, D> {
Expand Down Expand Up @@ -114,10 +61,10 @@ impl<'a, D: ContinuousDomain> Arrow<'a, D> {
pub fn is_coterminal(&self) -> bool {
self.arrow.is_none() && self.domain.is_empty() && !self.values.is_empty()
}
pub fn add_input(&mut self, connection: Connection<'a, D>) {
pub fn add_input(&mut self, connection: Variable<'a, D>) {
self.domain.push(connection);
}
pub fn add_output(&mut self, connection: Connection<'a, D>) {
pub fn add_output(&mut self, connection: Variable<'a, D>) {
self.codomain.push(connection);
}
pub fn inputs(&self) -> Vec<Option<D>> {
Expand Down Expand Up @@ -220,11 +167,11 @@ impl<'a, D: ContinuousDomain> Arrow<'a, D> {
#[cfg(test)]
mod tests {
use super::*;
use crate::func::*;
use crate::{func::*, DFN};
#[test]
fn test_connection_basic() {
let f0: Function<f64> = Function::coterminal(vec![0.0f64]);
let c0 = Connection::new(Some(0.0f64), &f0, &f0);
let c0 = Variable::new(Some(0.0f64), &f0, &f0);
let c1 = c0.clone();
c0.set_value(Some(10.0));
assert_eq!(c1.get_value(), Some(10.0));
Expand All @@ -237,13 +184,13 @@ mod tests {
let mut _a3: Arrow<f64> = Arrow::new(DFN!(|x| x - 1.0));
let f0: Function<f64> = Function::coterminal(vec![0.0f64]);
let f1: Function<f64> = Function::terminal(vec![1.0f64]);
let c0 = Connection::new(Some(0.0f64), &f0, &f1);
let c0 = Variable::new(Some(0.0f64), &f0, &f1);
a0.codomain.push(c0.clone());
assert!(a0.is_coterminal());
assert!(a0.is_applicable());
assert!(a0.is_applied());
assert!(a1.is_terminal());
a1.add_input(c0);
// let _c = Connection::new(0.0f64, &c0);
// let _c = Variable::new(0.0f64, &c0);
}
}
27 changes: 23 additions & 4 deletions dezorr/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

use {
crate::{
arrow::{Arrow, ArrowType, Connection},
arrow::{Arrow, ArrowType},
types::ContinuousDomain,
var::Variable,
DFN,
},
std::{cell::RefCell, collections::VecDeque},
Expand All @@ -30,6 +31,8 @@ pub trait FunctionOn<'a, D: ContinuousDomain> {
fn coterminal(value: Vec<D>) -> Self;
fn terminal(value: Vec<D>) -> Self;
fn is_coterminal(&'a self) -> bool;
fn add_input(&'a self, var: &Variable<'a, D>);
fn add_output(&'a self, var: &Variable<'a, D>);
fn link_to(&'a self, other: &'a Self);
fn propagate_forward(&'a self);
fn propagate_backward(&'a self);
Expand Down Expand Up @@ -101,21 +104,37 @@ impl<'a, D: ContinuousDomain> FunctionOn<'a, D> for Function<'a, D> {
let f = &binding.f;
f.is_coterminal()
}
fn add_input(&'a self, var: &Variable<'a, D>) {
{
let binding = &mut self.0.borrow_mut().f;
binding.add_input(var.clone());
}
}
fn add_output(&'a self, var: &Variable<'a, D>) {
{
let binding = &mut self.0.borrow_mut().f;
binding.add_output(var.clone());
}
}
fn link_to(&'a self, target: &'a Self) {
{
// forward bonding
let source_binding = &mut self.0.borrow_mut().f;
let dist_binding = &mut target.0.borrow_mut().f;
// assert!(source_binding.codomain.len()< source_binding.values.len());
let link = Connection::new(None, self, target);
let link = Variable::new(None, self, target);
// self.add_output(&link);
// target.add_input(&link);
// // assert!(source_binding.codomain.len()< source_binding.values.len());
source_binding.add_output(link.clone());
dist_binding.add_input(link);
}
{
// backward bonding
let source_binding = &mut target.0.borrow_mut().b;
let dist_binding = &mut self.0.borrow_mut().b;
let link = Connection::new(None, target, self);
let link = Variable::new(None, target, self);
// target.add_output(&link);
// self.add_input(&link);
source_binding.add_output(link.clone());
dist_binding.add_input(link);
}
Expand Down
69 changes: 68 additions & 1 deletion dezorr/src/var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,70 @@
use crate::types::ContinuousDomain;
#![allow(dead_code)]

use {
crate::{func::Function, types::ContinuousDomain},
std::{
cell::{Ref, RefCell, RefMut},
rc::Rc,
},
};

#[macro_export]
macro_rules! DFN {
($f: expr) => {
Some(Box::new(|xs| {
xs.iter().map(|r| $f(r.clone())).collect::<Vec<_>>()
}))
};
}

#[macro_export]
macro_rules! TFN {
($f: expr) => {
Some(Box::new($f))
};
}

#[derive(Clone)]
pub struct VariableBody<'a, D: ContinuousDomain> {
pub value: Option<D>,
pub source: &'a Function<'a, D>,
pub target: &'a Function<'a, D>,
}

#[derive(Clone)]
pub struct Variable<'a, D: ContinuousDomain>(pub Rc<RefCell<VariableBody<'a, D>>>);

impl<'a, D: ContinuousDomain + std::fmt::Debug> std::fmt::Debug for Variable<'a, D> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
let binding = self.0.borrow();
f.debug_struct("Variable")
.field("vaule", &binding.value)
.finish()
}
}

impl<'a, D: ContinuousDomain> Variable<'a, D> {
pub fn new(value: Option<D>, source: &'a Function<'a, D>, target: &'a Function<'a, D>) -> Self {
Variable(Rc::new(RefCell::new(VariableBody {
value,
source,
target,
})))
}
pub fn get(&'a self) -> Ref<'a, VariableBody<'a, D>> {
self.0.borrow()
}
pub fn set(&'a self) -> RefMut<'a, VariableBody<'a, D>> {
self.0.borrow_mut()
}
pub fn get_value(&self) -> Option<D> {
self.0.borrow().value.clone()
}
pub fn set_value(&self, val: Option<D>) {
self.0.borrow_mut().value = val;
}
}
/*

#[derive(Clone, Default)]
pub struct Variable<D: ContinuousDomain> {
Expand Down Expand Up @@ -32,3 +98,4 @@ mod tests {
assert_eq!(v2.val, 2.0);
}
}
*/