@@ -2,6 +2,7 @@ module Sampling_compressor
22using Test, RLinearAlgebra, Random
33using StatsBase: ProbabilityWeights, sample
44import LinearAlgebra: mul!, Adjoint
5+ import SparseArrays: sprandn
56using .. FieldTest
67using .. ApproxTol
78
@@ -499,8 +500,139 @@ Random.seed!(2131)
499500 mul! (x, S' , yc, alpha, beta)
500501 @test x ≈ alpha * Sty_exact + beta * xc
501502 end
503+
504+ end
505+
506+ @testset " Left Cardinality Sparse" begin
507+ let a_matrix_rows = 20 ,
508+ a_matrix_cols = 12 ,
509+ comp_dim = 7 ,
510+ alpha = 2.5 ,
511+ beta = 1.5
512+
513+ # Setup matrices and vectors
514+ A = sprandn (a_matrix_rows, a_matrix_cols, .8 )
515+ B = sprandn (comp_dim, a_matrix_cols, .8 )
516+ C1 = sprandn (comp_dim, a_matrix_cols, .8 )
517+ C2 = sprandn (a_matrix_rows, a_matrix_cols, .8 )
518+ x = sprandn (a_matrix_rows, .8 )
519+ y = sprandn (comp_dim, .8 )
520+
521+ # Keep copies for 5-argument mul! verification
522+ C1c = deepcopy (C1)
523+ C2c = deepcopy (C2)
524+ yc = deepcopy (y)
525+ xc = deepcopy (x)
526+
527+ # Setup the Sampling compressor recipe
528+ S_info = Sampling (
529+ cardinality= Left (),
530+ compression_dim= comp_dim,
531+ distribution= Uniform (cardinality= Left (), replace= false )
532+ )
533+ S = complete_compressor (S_info, A)
534+
535+ # Calculate all ground truth results
536+ SA_exact = A[S. idx, :]
537+ StB_exact = zeros (a_matrix_rows, a_matrix_cols); for i in 1 : comp_dim; StB_exact[S. idx[i], :] = B[i, :]; end
538+ Sx_exact = x[S. idx]
539+ Sty_exact = zeros (a_matrix_rows); for i in 1 : comp_dim; Sty_exact[S. idx[i]] = y[i]; end
540+
541+ # Test '*' operations by comparing to ground truths
542+ @test S * A ≈ SA_exact
543+ @test S' * B ≈ StB_exact
544+ @test A' * S' ≈ SA_exact'
545+ @test B' * S ≈ StB_exact'
546+ @test S * x ≈ Sx_exact
547+ @test x' * S' ≈ Sx_exact'
548+ @test S' * y ≈ Sty_exact
549+ @test y' * S ≈ Sty_exact'
550+
551+ # Test the 5-argument mul!
552+ mul! (C1, S, A, alpha, beta)
553+ @test C1 ≈ alpha * SA_exact + beta * C1c
554+
555+ mul! (C2, S' , B, alpha, beta)
556+ @test C2 ≈ alpha * StB_exact + beta * C2c
557+
558+ mul! (y, S, xc, alpha, beta)
559+ @test y ≈ alpha * Sx_exact + beta * yc
560+
561+ mul! (x, S' , yc, alpha, beta)
562+ @test x ≈ alpha * Sty_exact + beta * xc
563+ end
564+ end
565+
566+ # Test multiplications with right compressors
567+ @testset " Right Cardinality" begin
568+ let n = 20 ,
569+ comp_dim = 10 ,
570+ alpha = 2.0 ,
571+ beta = 2.0
572+
573+ # Setup matrices and vectors with dimensions
574+ A = sprandn (n, comp_dim, .8 )
575+ B = sprandn (n, n, .8 )
576+ # C1 is for S'*A, C2 is for B*S
577+ C1 = sprandn (n, n, .8 )
578+ C2 = sprandn (n, comp_dim, .8 )
579+ x = sprandn (comp_dim, .8 )
580+ y = sprandn (n, .8 )
581+
582+ # Keep copies for 5-argument mul! verification
583+ C1c = deepcopy (C1)
584+ C2c = deepcopy (C2)
585+ yc = deepcopy (y)
586+ xc = deepcopy (x)
587+
588+ # Setup the Sampling compressor recipe. It's created from B, an n x n matrix.
589+ # The operator S will have conceptual dimensions (n x comp_dim).
590+ S_info = Sampling (
591+ cardinality= Right (),
592+ compression_dim= comp_dim,
593+ distribution= Uniform (cardinality= Right (), replace= false )
594+ )
595+ S = complete_compressor (S_info, B)
596+
597+ # Calculate all ground truth results based on direct indexing/operations
598+ StA_exact = A[S. idx, :]
599+ BS_exact = B[:, S. idx]
600+ BtS_exact = B' [:, S. idx]
601+ ASt_exact = zeros (n, n); for i in 1 : comp_dim; ASt_exact[:, S. idx[i]] = A[:, i]; end
602+ Sx_exact = zeros (n); for i in 1 : comp_dim; Sx_exact[S. idx[i]] = x[i]; end
603+ Sty_exact = y[S. idx]
604+
605+ # Test '*' operations by comparing to ground truths
606+ @test S' * A ≈ StA_exact
607+ @test A' * S ≈ StA_exact'
608+ @test B * S ≈ BS_exact
609+ @test S' * B' ≈ BS_exact'
610+ @test B' * S ≈ BtS_exact
611+ @test S' * B ≈ BtS_exact'
612+ @test A * S' ≈ ASt_exact
613+ @test S * A' ≈ ASt_exact'
614+ @test S * x ≈ Sx_exact
615+ @test x' * S' ≈ Sx_exact'
616+ @test y' * S ≈ Sty_exact'
617+ @test S' * y ≈ Sty_exact
618+
619+ # Test the 5-argument mul!
620+ mul! (C1, A, S' , alpha, beta)
621+ @test C1 ≈ alpha * ASt_exact + beta * C1c
622+
623+ mul! (C2, B, S, alpha, beta)
624+ @test C2 ≈ alpha * BS_exact + beta * C2c
625+
626+ mul! (y, S, xc, alpha, beta)
627+ @test y ≈ alpha * Sx_exact + beta * yc
628+
629+ mul! (x, S' , yc, alpha, beta)
630+ @test x ≈ alpha * Sty_exact + beta * xc
631+ end
632+
502633 end
503634 end
635+
504636end
505637
506638end
0 commit comments