Posted to tcl by florentis at Sun Oct 04 03:35:09 GMT 2026view raw
- # adapted from tcllib/math/linalg.tcl
-
- proc ::math::linearalgebra::dim {obj} {(
- shape=[shape $obj];
- $shape != 1 ? return [llength [shape $obj]]
- : return 0
- )}
-
- proc ::math::linearalgebra::shape {obj} {(
- result=llength $obj;
- llength [lindex $obj 0] <= 1 ? return $result
- : lappend result [llength [lindex $obj 0]];
- return $result
- )}
-
- proc ::math::linearalgebra::dotproduct {vect1 vect2} {(
- llength $vect1 != llength $vect2 ?
- return \-code error "Vectors must be of equal length":;
-
- sum=0.0;
- foreach c1 $vect1 c2 $vect2 {(
- sum=$sum + $c1*$c2
- )}
- return $sum
- )}
-
- proc ::math::linearalgebra::crossproduct {U V} {(
- llength $U == 3 && llength $V == 3 ? (
- lassign $U ux uy uz , lassign $V vx vy vz;
- return [(
- $uy*$vz - $uz*$vy,
- $uz*$vx - $ux*$vz,
- $vx*$vy - $vy*$vx
- )];
- ) : return \-code error "Cross-product only defined for 3D vectors";
- )}
-
- proc ::math::linearalgebra::conforming { type obj1 obj2 } {(
- shape1=[shape $obj1];
- shape2=[shape $obj2];
- result=0;
-
- result = switch $type {
- shape {(
- lindex $shape1 0 == lindex $shape2 0
- && lindex $shape1 1 == lindex $shape2 1
- )}
- rows {( lindex $shape1 0 == lindex $shape2 0 )}
- matmul {(
- llength $shape1 == 2 ? ( lindex $shape1 1 == lindex $shape2 0)
- : llength $shape2 == 2 ? (lindex $shape1 0 == lindex $shape2 0)
- : (lindex $shape1 0 == lindex $shape2 0)
- )}
- default {
- return -code error "Unknown type of conforming check - $type - should be one of: shape rows matmul"
- }
- };
- return $result
- )}
-
Add a comment