Added 'diag' function

This commit is contained in:
bakk 2022-01-20 20:49:00 +01:00 committed by PaddiM8
parent 2699ce8fee
commit 2bda63b058

View File

@ -103,6 +103,7 @@ lazy_static! {
pub static ref VECTOR_FUNCS: HashMap<&'static str, VectorFuncInfo> = {
let mut m = HashMap::new();
m.insert("average", VectorFuncInfo(average, Other));
m.insert("diag", VectorFuncInfo(diag, Other));
m.insert("max", VectorFuncInfo(max, Other));
m.insert("min", VectorFuncInfo(min, Other));
m
@ -537,6 +538,19 @@ pub mod funcs {
)
}
pub fn diag(x: KalkValue) -> KalkValue {
if let KalkValue::Vector(values) = x {
let mut result = vec![vec![KalkValue::from(0f64); values.len()]; values.len()];
for (i, value) in values.iter().enumerate() {
result[i][i] = value.clone();
}
KalkValue::Matrix(result)
} else {
KalkValue::nan()
}
}
pub fn exp(x: KalkValue) -> KalkValue {
let has_imaginary = x.has_imaginary();
let (real, imaginary, unit) = as_number_or_return!(x);