@@ -832,3 +832,150 @@ describe('OnesMatrix', () => {
832832 ) ;
833833 } ) ;
834834} ) ;
835+
836+ describe ( 'Norm' , ( ) => {
837+ // Scalar norm (absolute value)
838+ it ( 'should compute the norm of a scalar' , ( ) => {
839+ const result = ce . box ( [ 'Norm' , 5 ] ) . evaluate ( ) ;
840+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `5` ) ;
841+ } ) ;
842+
843+ it ( 'should compute the norm of a negative scalar' , ( ) => {
844+ const result = ce . box ( [ 'Norm' , - 7 ] ) . evaluate ( ) ;
845+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `7` ) ;
846+ } ) ;
847+
848+ // Vector L2 norm (default)
849+ it ( 'should compute the L2 norm of a vector (3-4-5 triangle)' , ( ) => {
850+ // √(3² + 4²) = √(9 + 16) = √25 = 5
851+ const result = ce . box ( [ 'Norm' , [ 'List' , 3 , 4 ] ] ) . evaluate ( ) ;
852+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `5` ) ;
853+ } ) ;
854+
855+ it ( 'should compute the L2 norm of a vector with negatives' , ( ) => {
856+ // √(3² + (-4)²) = 5
857+ const result = ce . box ( [ 'Norm' , [ 'List' , 3 , - 4 ] ] ) . evaluate ( ) ;
858+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `5` ) ;
859+ } ) ;
860+
861+ it ( 'should compute the L2 norm of a 3D vector' , ( ) => {
862+ // √(1² + 2² + 2²) = √(1 + 4 + 4) = √9 = 3
863+ const result = ce . box ( [ 'Norm' , [ 'List' , 1 , 2 , 2 ] ] ) . evaluate ( ) ;
864+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `3` ) ;
865+ } ) ;
866+
867+ // Vector L1 norm
868+ it ( 'should compute the L1 norm of a vector' , ( ) => {
869+ // |3| + |-4| = 3 + 4 = 7
870+ const result = ce . box ( [ 'Norm' , [ 'List' , 3 , - 4 ] , 1 ] ) . evaluate ( ) ;
871+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `7` ) ;
872+ } ) ;
873+
874+ it ( 'should compute the L1 norm of a longer vector' , ( ) => {
875+ // |1| + |-2| + |3| + |-4| = 1 + 2 + 3 + 4 = 10
876+ const result = ce . box ( [ 'Norm' , [ 'List' , 1 , - 2 , 3 , - 4 ] , 1 ] ) . evaluate ( ) ;
877+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `10` ) ;
878+ } ) ;
879+
880+ // Vector L∞ norm (max absolute value)
881+ it ( 'should compute the L-infinity norm of a vector' , ( ) => {
882+ // max(|3|, |-4|) = 4
883+ const result = ce
884+ . box ( [ 'Norm' , [ 'List' , 3 , - 4 ] , 'Infinity' ] )
885+ . evaluate ( ) ;
886+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `4` ) ;
887+ } ) ;
888+
889+ it ( 'should compute the L-infinity norm with string' , ( ) => {
890+ // max(|1|, |-5|, |3|) = 5
891+ const result = ce
892+ . box ( [ 'Norm' , [ 'List' , 1 , - 5 , 3 ] , { str : 'Infinity' } ] )
893+ . evaluate ( ) ;
894+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `5` ) ;
895+ } ) ;
896+
897+ // Vector Lp norm (general)
898+ it ( 'should compute the L3 norm of a vector' , ( ) => {
899+ // (|3|³ + |4|³)^(1/3) = (27 + 64)^(1/3) = 91^(1/3) ≈ 4.498
900+ const result = ce . box ( [ 'Norm' , [ 'List' , 3 , 4 ] , 3 ] ) . evaluate ( ) ;
901+ expect ( result . re ) . toBeCloseTo ( 4.4979 , 3 ) ;
902+ } ) ;
903+
904+ it ( 'should compute the L4 norm of a vector' , ( ) => {
905+ // (|2|⁴ + |2|⁴)^(1/4) = (16 + 16)^(1/4) = 32^(1/4) ≈ 2.378
906+ const result = ce . box ( [ 'Norm' , [ 'List' , 2 , 2 ] , 4 ] ) . evaluate ( ) ;
907+ expect ( result . re ) . toBeCloseTo ( 2.3784 , 3 ) ;
908+ } ) ;
909+
910+ // Matrix Frobenius norm (default)
911+ it ( 'should compute the Frobenius norm of a matrix' , ( ) => {
912+ // √(1² + 2² + 3² + 4²) = √(1 + 4 + 9 + 16) = √30 ≈ 5.477
913+ const result = ce . box ( [ 'Norm' , sq2_n ] ) . evaluate ( ) ;
914+ expect ( result . re ) . toBeCloseTo ( 5.4772 , 3 ) ;
915+ } ) ;
916+
917+ it ( 'should compute the Frobenius norm of a non-square matrix' , ( ) => {
918+ // √(1² + 2² + 3² + 4² + 5² + 6²) = √(1+4+9+16+25+36) = √91 ≈ 9.539
919+ const result = ce . box ( [ 'Norm' , m23_n ] ) . evaluate ( ) ;
920+ expect ( result . re ) . toBeCloseTo ( 9.5394 , 3 ) ;
921+ } ) ;
922+
923+ it ( 'should compute the Frobenius norm with explicit type' , ( ) => {
924+ const result = ce
925+ . box ( [ 'Norm' , sq2_n , { str : 'Frobenius' } ] )
926+ . evaluate ( ) ;
927+ expect ( result . re ) . toBeCloseTo ( 5.4772 , 3 ) ;
928+ } ) ;
929+
930+ // Matrix L1 norm (max column sum)
931+ it ( 'should compute the L1 norm of a matrix' , ( ) => {
932+ // [[1, 2], [3, 4]]
933+ // Column sums: |1| + |3| = 4, |2| + |4| = 6
934+ // max = 6
935+ const result = ce . box ( [ 'Norm' , sq2_n , 1 ] ) . evaluate ( ) ;
936+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `6` ) ;
937+ } ) ;
938+
939+ it ( 'should compute the L1 norm of a matrix with negatives' , ( ) => {
940+ // [[1, -2], [-3, 4]]
941+ // Column sums: |1| + |-3| = 4, |-2| + |4| = 6
942+ // max = 6
943+ const result = ce
944+ . box ( [ 'Norm' , [ 'List' , [ 'List' , 1 , - 2 ] , [ 'List' , - 3 , 4 ] ] , 1 ] )
945+ . evaluate ( ) ;
946+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `6` ) ;
947+ } ) ;
948+
949+ // Matrix L∞ norm (max row sum)
950+ it ( 'should compute the L-infinity norm of a matrix' , ( ) => {
951+ // [[1, 2], [3, 4]]
952+ // Row sums: |1| + |2| = 3, |3| + |4| = 7
953+ // max = 7
954+ const result = ce
955+ . box ( [ 'Norm' , sq2_n , { str : 'Infinity' } ] )
956+ . evaluate ( ) ;
957+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `7` ) ;
958+ } ) ;
959+
960+ it ( 'should compute the L-infinity norm of a non-square matrix' , ( ) => {
961+ // [[1, 2, 3], [4, 5, 6]]
962+ // Row sums: 1 + 2 + 3 = 6, 4 + 5 + 6 = 15
963+ // max = 15
964+ const result = ce
965+ . box ( [ 'Norm' , m23_n , { str : 'Infinity' } ] )
966+ . evaluate ( ) ;
967+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `15` ) ;
968+ } ) ;
969+
970+ // Zero vector
971+ it ( 'should compute the norm of a zero vector' , ( ) => {
972+ const result = ce . box ( [ 'Norm' , [ 'List' , 0 , 0 , 0 ] ] ) . evaluate ( ) ;
973+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `0` ) ;
974+ } ) ;
975+
976+ // Single element vector
977+ it ( 'should compute the norm of a single element vector' , ( ) => {
978+ const result = ce . box ( [ 'Norm' , [ 'List' , - 5 ] ] ) . evaluate ( ) ;
979+ expect ( result . toString ( ) ) . toMatchInlineSnapshot ( `5` ) ;
980+ } ) ;
981+ } ) ;
0 commit comments