R Under development (unstable) (2024-01-26 r85832 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > library( colorSpec ) Attaching colorSpec. Version: 1.5-0. Author: Glenn Davis [aut, cre]. Built: R 4.4.0; ; 2024-01-28 04:15:05 UTC; windows > > > # n dimension of n-cube > # sd standard-deviation of smoothing filter > # scale bigger means more 0's and 1's > # > # returns a random point in the n-cube, with emphasis on lots of 0's and 1's > # > # to get no 0's and 1's set sd=0 and scale=1 > randomSpectrum <- function( n, sd=5, scale=100 ) + { + out = stats::runif( n ) + + if( 0 < sd ) + { + kern = stats::dnorm( seq(-3*sd,3*sd,len=n/2+1), sd=sd ) + kern = kern / sum(kern) + # out = out[ is.finite(out) ] not needed when circular + out = stats::filter( out, kern, circular=TRUE ) #; print(out) + out = as.numeric(out) + } + + out = pmin( pmax( scale*(out-0.5) + 0.5, 0 ), 1 ) + + return( out ) + } > > > testBands <- function( n, samples, sd=5, scale=100, tol=5.e-12 ) + { + set.seed(0) + + wave = 400:(400+n-1) + + mat = matrix( NA_real_, n, samples ) + + for( j in 1:samples ) + { + mat[ ,j] = randomSpectrum( n, sd=sd, scale=scale ) # ; print( spec ) + } + + obj = colorSpec( mat, wave, quantity='transmittance', specnames=sprintf( "random-%d", 1:samples ) ) + + # spectra to a list of matrices + lambdalist = bandRepresentation( obj ) + if( is.null(lambdalist) ) return(FALSE) + # print( lambdalist ) + + # list of matrices back to spectra + obj.back = bandMaterial( lambdalist, wave ) + if( is.null(obj.back) ) return(FALSE) + + + # now compare the two + transmax = 0 + transmin = Inf + delta = numeric(samples) + + mat.back = as.matrix( obj.back ) + + for( j in 1:samples ) + { + spec = mat[ ,j] + + spec.back = mat.back[ ,j] + + delta[j] = max( abs(spec - spec.back) ) + + lambdamat = lambdalist[[j]] + + if( tol < delta[j] ) + { + cat( "----------------\n" ) + print( spec ) + print( lambdamat ) + print( spec.back ) + } + + transmin = min( length(lambdamat), transmin ) + transmax = max( length(lambdamat), transmax ) + } + + count = sum( tol < delta ) + mess = sprintf( "%d violations of %d samples (delta > %g). max(delta)=%g", + count, samples, tol, max(delta) ) + cat( mess, '\n' ) + mess = sprintf( "transition count range: %d to %d.", transmin, transmax ) + cat( mess, '\n' ) + + return( count == 0 ) + } > > > > if( ! testBands( 50, 1000 ) ) stop( "testBands() failed !" ) 0 violations of 1000 samples (delta > 5e-12). max(delta)=6.88338e-14 transition count range: 0 to 18. > > cat( "Passed all bands tests !\n", file=stderr() ) Passed all bands tests ! > > proc.time() user system elapsed 0.85 0.10 0.93