├── .dockerignore ├── .gitignore ├── .travis.yml ├── AUTHORS ├── BUGS ├── BUILDING_ATLAS ├── COPYING ├── Dockerfile ├── INSTALL ├── Makefile ├── README.md ├── RELEASE_NOTES ├── ROADMAP ├── build.xml ├── config ├── PrintProperty.java ├── arch_flavor.c ├── config.rb ├── config_cc.rb ├── config_fortran.rb ├── config_java.rb ├── config_lapack_sources.rb ├── config_libs.rb ├── config_make.rb ├── config_os_arch.rb ├── config_tools.rb ├── configure.rb ├── lib_helpers.rb ├── opts.rb ├── path.rb ├── string_ext.rb └── windows.rb ├── configure ├── docker ├── dev-ubuntu1804-arm64.Dockerfile ├── dev-ubuntu1804.Dockerfile ├── dev-ubuntu2004-arm64.Dockerfile ├── dev-ubuntu2004.Dockerfile ├── test-ubuntu1804-arm64.Dockerfile ├── test-ubuntu1804.Dockerfile ├── test-ubuntu2004-arm64.Dockerfile └── test-ubuntu2004.Dockerfile ├── docs ├── jblas-logo-64.png ├── jblas-logo-square.png ├── jblas-logo.png ├── jblas-logo.svg ├── jblas-thumb.png ├── render.rb └── tutorial.textile ├── examples ├── complex.rb ├── complex_svd.rb ├── jmatrix.rb ├── test_abitofml.rb ├── test_matrix.rb ├── test_time_eigen.rb └── tictoc.rb ├── fortranwrapper.dump ├── pom.xml ├── scripts ├── add-license.rb ├── c-file.c ├── c-header.h ├── class_to_float.rb ├── fortran │ ├── java.rb │ ├── parser.rb │ └── types.rb ├── fortranwrapper.rb ├── java-class.java ├── java-impl.c ├── render-textile.rb ├── rjpp.rb ├── static_class_to_float.rb ├── template_context.rb └── test_fortranwrapper └── src ├── main ├── c │ ├── NativeBlas.c │ ├── jblas_arch_flavor.c │ ├── org_jblas_NativeBlas.h │ └── org_jblas_util_ArchFlavor.h ├── java │ └── org │ │ └── jblas │ │ ├── ComplexDouble.java │ │ ├── ComplexDoubleMatrix.java │ │ ├── ComplexFloat.java │ │ ├── ComplexFloatMatrix.java │ │ ├── ConvertsToDoubleMatrix.java │ │ ├── ConvertsToFloatMatrix.java │ │ ├── Decompose.java │ │ ├── DoubleFunction.java │ │ ├── DoubleMatrix.java │ │ ├── Eigen.java │ │ ├── FloatFunction.java │ │ ├── FloatMatrix.java │ │ ├── Geometry.java │ │ ├── Info.java │ │ ├── JavaBlas.java │ │ ├── MatrixFunctions.java │ │ ├── NativeBlas.java │ │ ├── NativeBlasLibraryLoader.java │ │ ├── SimpleBlas.java │ │ ├── Singular.java │ │ ├── Solve.java │ │ ├── Trigonometry.java │ │ ├── benchmark │ │ ├── Benchmark.java │ │ ├── BenchmarkResult.java │ │ ├── JavaDoubleMultiplicationBenchmark.java │ │ ├── JavaFloatMultiplicationBenchmark.java │ │ ├── Main.java │ │ ├── NativeDoubleMultiplicationBenchmark.java │ │ ├── NativeFloatMultiplicationBenchmark.java │ │ ├── Timer.java │ │ └── package-info.java │ │ ├── exceptions │ │ ├── LapackArgumentException.java │ │ ├── LapackConvergenceException.java │ │ ├── LapackException.java │ │ ├── LapackPositivityException.java │ │ ├── LapackSingularityException.java │ │ ├── NoEigenResultException.java │ │ ├── SizeException.java │ │ ├── UnsupportedArchitectureException.java │ │ └── package-info.java │ │ ├── package-info.java │ │ ├── ranges │ │ ├── AllRange.java │ │ ├── IndicesRange.java │ │ ├── IntervalRange.java │ │ ├── PointRange.java │ │ ├── Range.java │ │ ├── RangeUtils.java │ │ └── package-info.java │ │ └── util │ │ ├── ArchFlavor.java │ │ ├── Functions.java │ │ ├── LibraryLoader.java │ │ ├── Logger.java │ │ ├── Permutations.java │ │ ├── Random.java │ │ ├── SanityChecks.java │ │ └── package-info.java ├── javadoc │ ├── overview.html │ └── stylesheet.css └── resources │ └── lib │ └── static │ ├── Linux │ ├── aarch64 │ │ ├── libjblas.so │ │ └── libjblas_arch_flavor.so │ ├── amd64 │ │ ├── libgfortran-4.so │ │ ├── libjblas_arch_flavor.so │ │ ├── libquadmath-0.so │ │ └── sse3 │ │ │ └── libjblas.so │ ├── i386 │ │ ├── libjblas_arch_flavor.so │ │ └── sse3 │ │ │ └── libjblas.so │ └── ppc64le │ │ ├── libjblas.so │ │ └── libjblas_arch_flavor.so │ ├── Mac OS X │ ├── aarch64 │ │ ├── libjblas.jnilib │ │ └── libjblas_arch_flavor.jnilib │ └── x86_64 │ │ ├── libjblas_arch_flavor.jnilib │ │ └── sse3 │ │ └── libjblas.jnilib │ └── Windows │ ├── amd64 │ ├── jblas.dll │ ├── jblas_arch_flavor.dll │ ├── libgcc_s_sjlj-1.dll │ └── libgfortran-3.dll │ └── x86 │ ├── jblas_arch_flavor.dll │ ├── libgcc_s_dw2-1.dll │ ├── libgfortran-3.dll │ └── sse3 │ └── jblas.dll └── test └── java └── org └── jblas ├── AbstractTestJblas.java ├── JblasAssert.java ├── SimpleBlasTest.java ├── TestBlasDouble.java ├── TestBlasDoubleComplex.java ├── TestBlasFloat.java ├── TestComplexDoubleMatrix.java ├── TestComplexFloat.java ├── TestDecompose.java ├── TestDoubleMatrix.java ├── TestEigen.java ├── TestFloatMatrix.java ├── TestGeometry.java ├── TestSingular.java ├── TestSolve.java └── ranges └── RangeTest.java /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | src 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # maven target repo 2 | 3 | target/ 4 | 5 | # idea files 6 | .idea/ 7 | *.ipr 8 | *.iml 9 | *.iws 10 | 11 | *.rjpp 12 | *.class 13 | configure.log 14 | configure.options 15 | config/arch_flavor 16 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | notifications: 3 | email: mikiobraun@gmail.com 4 | on_success: never 5 | on_failure: always 6 | 7 | before_install: 8 | - sudo apt-get install -q libgfortran3 9 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Main Developer: 2 | 3 | Mikio L. Braun 4 | 5 | Additional Programming and Contributions (not all, only significant code additions) 6 | (unless noted diffently, funny looking names are github user names) 7 | 8 | Johannes Schaback 9 | Jan Saputra Müller (exponential matrix multiplication, decomposition) 10 | Matthias L. Jugel (packaging) 11 | Nicolas Oury (generalized eigenvectors) 12 | http://github.com/cheshirekow (fixed with range objects) 13 | Quantisan (travis integration) 14 | robbymckilliam (fixes with complex SVD) 15 | srowen (various performance fixes) 16 | mtbrandy (ppc64le atlas libraries) 17 | jey (adding orgqr) 18 | lifeiteng (fixing SimpleBlas.gemv) 19 | sehlstrom (range clarification for Eigen) 20 | chmp (doc updates) 21 | mpoussevain (Tests and French locale) 22 | -------------------------------------------------------------------------------- /BUGS: -------------------------------------------------------------------------------- 1 | Known Bugs: 2 | 3 | - tempfiles don't get deleted under Windows 4 | 5 | Under Windows, the JVM cannot delete the temp files used to extract the data 6 | once these files are linked because you cannot delete a file as long as 7 | someone has it opened. 8 | 9 | Workaround is to clear out the temp directory from time to time. 10 | 11 | - XERBLA handling under Mac OS X (still current?) 12 | 13 | Under Mac OS X, xerbla isn't properly handled, leading to an error message 14 | and the exit of the program. The linker does not take our own version 15 | of xerbla, but instead uses the one supplied by the BLAS library. I 16 | still need to figure out what the right options are. 17 | 18 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009, Mikio L. Braun and contributors 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | * Neither the name of the Technische Universitaet Berlin nor the 17 | names of its contributors may be used to endorse or promote 18 | products derived from this software without specific prior 19 | written permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:11 2 | 3 | ENV JBLAS_VERSION=1.2.4-SNAPSHOT 4 | 5 | RUN apt-get update 6 | RUN apt-get -y install libgfortran5 7 | 8 | COPY jblas-1.2.4-SNAPSHOT.jar . 9 | 10 | CMD java -jar jblas-${JBLAS_VERSION}.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jblas is a matrix library for Java which uses existing high 2 | performance BLAS and LAPACK libraries like ATLAS. 3 | 4 | * Version 1.2.5, August 22, 2020 5 | * Version 1.2.4, May 11, 2015 6 | * Version 1.2.3, February 13, 2013 7 | * Version 1.2.2, December 17, 2012 8 | * Version 1.2.1 9 | * Version 1.2.0, January 7, 2011 10 | * Version 1.1.1 11 | * Version 1.1, August 16, 2010 12 | * Version 1.0.2, February 26, 2010 13 | * Version 1.0.1, January 14, 2010 14 | * Version 1.0, December 22, 2009 15 | * Version 0.3, September 17, 2009 16 | * Version 0.2, May 8, 2009 17 | * Version 0.1, March 28, 2009 18 | 19 | see also the file RELEASE_NOTES 20 | 21 | Homepage: http://jblas.org 22 | 23 | INSTALL 24 | ------- 25 | 26 | In principle, all you need is the jblas-1.2.5.jar in your 27 | classpath. jblas-1.2.5.jar will then automagically extract your platform 28 | dependent native library to a tempfile and load it from there. You can 29 | also put that file somewhere in your load path ($LD_LIBRARY_PATH for 30 | Linux, %PATH for Windows). 31 | 32 | Or, use the following dependency in maven 33 | 34 | ```HTML 35 | 36 | org.jblas 37 | jblas 38 | 1.2.5 39 | 40 | ``` 41 | 42 | BUILDING 43 | -------- 44 | 45 | If you only work on the java part, you can use maven to recompile from the sources. 46 | In addition to that you need an installation of 47 | ruby for some scripts which automaticall generate code. Then, you just 48 | type "mvn package" on the command line. 49 | 50 | If you want to build jblas from the sources including the native part, 51 | you need to set up quite a few things: 52 | 53 | You will need some implementation of blas and lapack. jblas is tested 54 | with either plain lapack, or ATLAS 55 | (http://math-atlas.sourceforge.net/). You also need the Fortran 56 | sources for BLAS and LAPACK, available, for example from 57 | http://www.netlib.org/lapack/lapack-lite-3.1.1.tgz. 58 | 59 | If you still want to build the source your own, see INSTALL for 60 | further details. 61 | 62 | 63 | HOW TO GET STARTED 64 | ------------------ 65 | 66 | Have a look at javadoc/index.html and 67 | javadoc/org/jblas/DoubleMatrix.html 68 | 69 | If you want to validate your installation and get some performance 70 | numbers, try "java -jar jblas-1.2.5.jar". 71 | 72 | 73 | LICENSE 74 | ------- 75 | 76 | jblas is distributed under a BSD-style license. See the file COPYING 77 | for more information. 78 | 79 | 80 | BUGS 81 | ---- 82 | 83 | If you encounter any bugs, feel free to go to http://jblas.org and 84 | register a ticket for them. Make sure to include as much information 85 | as possible. For configuration problems it would also be helpful to 86 | include the file "configure.log". 87 | 88 | 89 | CONTRIBUTORS 90 | ------------ 91 | 92 | see file AUTHORS -------------------------------------------------------------------------------- /ROADMAP: -------------------------------------------------------------------------------- 1 | Roadmap to jblas 1.2.1 2 | 3 | 1. Linux / 32 bit / sse2 4 | 2. Linux / 32 bit / sse3 5 | 3. Linux / 64 bit / sse2 6 | 4. Linux / 64 bit / sse3 7 | 5. Mac OS X / 64 bit / sse3 8 | 6. Windows / 32 bit / sse2 9 | 7. Windows / 32 bit / sse3 10 | 8. Windows / 64 bit / lapack lite only 11 | 12 | Roadmap to jblas-0.3.1 13 | 14 | - collect static builds for Windows(32), Linux(32/64), MacOSX(32/64). 15 | 16 | - create multiplatform jar (for all). 17 | 18 | - little benchmarking and testing tool. 19 | 20 | - update README, INSTALL, RELEASE_NOTES -------------------------------------------------------------------------------- /config/PrintProperty.java: -------------------------------------------------------------------------------- 1 | public class PrintProperty { 2 | public static void main(String[] args) { 3 | for (String arg: args) { 4 | System.out.println(System.getProperty(arg)); 5 | } 6 | } 7 | } -------------------------------------------------------------------------------- /config/arch_flavor.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Thanks to the following pages for helping me out on this. 4 | * 5 | * http://softpixel.com/~cwright/programming/simd/cpuid.php 6 | * http://www.gentoo.org/proj/en/hardened/pic-fix-guide.xml 7 | */ 8 | #define cpuid(func,ax,bx,cx,dx) \ 9 | __asm__ __volatile__ (" \ 10 | movl %%ebx, %%edi; \ 11 | cpuid; \ 12 | movl %%ebx, %1; \ 13 | movl %%edi, %%ebx" \ 14 | : "=a" (ax), "=r" (bx), "=c" (cx), "=d" (dx) \ 15 | : "a" (func) \ 16 | : "edi"); 17 | 18 | /* in edx */ 19 | #define SSE (1L << 25) 20 | #define SSE2 (1L << 26) 21 | 22 | /* in ecx */ 23 | #define SSE3 (1L << 0) 24 | 25 | int sse_level() { 26 | int level = -1; 27 | int a, b, c, d; 28 | 29 | cpuid(1, a, b, c, d); 30 | 31 | if (d & SSE) { 32 | level = 1; 33 | } 34 | 35 | if (d & SSE2) { 36 | level = 2; 37 | } 38 | 39 | if (c & SSE3) { 40 | level = 3; 41 | } 42 | return level; 43 | } 44 | 45 | int main(int argc, char **argv) { 46 | switch (sse_level()) { 47 | case 1: 48 | printf("sse\n"); 49 | break; 50 | case 2: 51 | printf("sse2\n"); 52 | break; 53 | case 3: 54 | printf("sse3\n"); 55 | break; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /config/config_cc.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | require 'config/path' 36 | require 'config/config' 37 | require 'config/config_java' 38 | 39 | include JblasConfig 40 | include Path 41 | 42 | # Set up flags for different environments. 43 | configure :cc => 'CC' 44 | 45 | desc 'Setting up gcc and flags' 46 | configure 'CC', 'CFLAGS' => ['OS_NAME', 'OS_ARCH', 'JAVA_HOME'] do 47 | os_name = JblasConfig::CONFIG['OS_NAME'] 48 | java_home = JblasConfig::CONFIG['JAVA_HOME'] 49 | case os_name 50 | when 'Linux' 51 | Path.check_cmd('gcc', 'make', 'ld') 52 | JblasConfig::CONFIG << < ['F77', 'LD'] 44 | 45 | desc 'deciding whether to use g77 or gfortran' 46 | configure 'F77', 'LD' => ['OS_NAME', 'CC'] do 47 | #unless ENV['CC'].nil? 48 | # CONFIG['CC'] = ENV['CC'] 49 | # return 50 | #end 51 | 52 | if CONFIG['OS_NAME'] == 'Mac\ OS\ X' 53 | CONFIG['LD'] = CONFIG['CC'] 54 | CONFIG['F77'] = 'gfortran' 55 | CONFIG['CCC'] = 'c99' 56 | elsif CONFIG['OS_NAME'] == 'Windows' 57 | if CONFIG['OS_ARCH'] == 'amd64' 58 | CONFIG['LD'] = W64_PREFIX + 'gfortran' 59 | CONFIG['F77'] = W64_PREFIX + 'gfortran' 60 | CONFIG['CCC'] = 'c99' 61 | else 62 | CONFIG['LD'] = W32_PREFIX + 'gfortran' 63 | CONFIG['F77'] = W32_PREFIX + 'gfortran' 64 | CONFIG['CCC'] = 'c99' 65 | end 66 | else 67 | g77 = Path.where('g77') 68 | gfortran = Path.where('gfortran') 69 | f77 = Path.where('f77') 70 | if g77 71 | CONFIG['LD'] = 'g77' 72 | CONFIG['F77'] = 'g77' 73 | CONFIG['CCC'] = 'f2c' 74 | elsif gfortran 75 | CONFIG['F77'] = 'gfortran' 76 | CONFIG['LD'] = CONFIG['CC'] 77 | CONFIG['CCC'] = 'c99' 78 | elsif f77 79 | CONFIG['F77'] = 'f77' 80 | CONFIG['LD'] = 'f77' 81 | CONFIG['CCC'] = 'f2c' 82 | else 83 | CONFIG.fail < ['FOUND_JAVA', 'JAVA_HOME'] 44 | 45 | desc 'checking for java, javac' 46 | configure 'FOUND_JAVA' do 47 | check_cmd('java', 'javac') 48 | %x(javac config/PrintProperty.java) 49 | CONFIG['FOUND_JAVA'] = true 50 | ok 51 | end 52 | 53 | desc 'locating the Java Development Kit' 54 | configure 'JAVA_HOME' => ['FOUND_JAVA', 'OS_NAME'] do 55 | if ENV.include? 'JAVA_HOME' 56 | java_home = ENV['JAVA_HOME'] 57 | else 58 | java_home = dir(%x(java -cp config PrintProperty java.home).chomp.gsub(/\/jre$/, '')) 59 | end 60 | #if CONFIG['OS_NAME'] == 'Mac\ OS\ X' 61 | # java_home = File.join(java_home, 'Home') 62 | #end 63 | check_files java_home, ['include', 'jni.h'] do 64 | CONFIG['JAVA_HOME'] = java_home #.escape 65 | end 66 | ok(java_home) 67 | end 68 | 69 | if __FILE__ == $0 70 | ConfigureTask.run :java 71 | end 72 | -------------------------------------------------------------------------------- /config/config_lapack_sources.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | require 'config/config' 36 | require 'config/path' 37 | require 'config/opts' 38 | require 'config/string_ext' 39 | 40 | include JblasConfig 41 | 42 | configure :lapack_sources => 'LAPACK_HOME' 43 | def check_lapack_home(lapack_home) 44 | Path.check_files(lapack_home, 45 | ['BLAS', 'SRC', 'dgemm.f'], 46 | ['SRC', 'dsyev.f']) 47 | end 48 | 49 | desc 'search for lapack sources (configure by --lapack=dir)' 50 | configure 'LAPACK_HOME' do 51 | lapack_home = $opts.get :lapack, './lapack-lite-3.1.1' 52 | begin 53 | check_lapack_home(lapack_home) 54 | rescue ConfigError => e 55 | if $opts.defined? :download_lapack 56 | puts "trying to download lapack (about 5M)" 57 | print "Looking for wget..."; check_cmd 'wget'; JblasConfig.ok 58 | lapack_tgz = File.join('.', 'lapack-lite-3.1.1.tgz') 59 | File.delete(lapack_tgz) if File.exist?(lapack_tgz) 60 | system("wget http://www.netlib.org/lapack/lapack-lite-3.1.1.tgz") 61 | system("tar xzvf lapack-lite-3.1.1.tgz") 62 | check_lapack_home(lapack_home) 63 | else 64 | CONFIG['LAPACK_HOME'] = '' 65 | puts < ['MAKE'] 42 | 43 | desc 'looking for version of make' 44 | configure 'MAKE' do 45 | if Path.where('gmake') 46 | puts 'gmake' 47 | CONFIG['MAKE'] = 'gmake' 48 | else 49 | if Path.where_with_output('make -v', /GNU Make/).nil? 50 | JblasConfig.fail('I need GNU make to run...') 51 | end 52 | CONFIG['MAKE'] = 'make' 53 | end 54 | ok(CONFIG['MAKE']) 55 | end 56 | 57 | if __FILE__ == $0 58 | ConfigureTask.run :make 59 | end -------------------------------------------------------------------------------- /config/config_os_arch.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | # 36 | # Check the Operating System 37 | # 38 | 39 | require 'config/config' 40 | require 'config/string_ext' 41 | require 'config/config_java' 42 | 43 | include JblasConfig 44 | 45 | def detect_os 46 | os_name = %x(java -cp config PrintProperty os.name).chomp 47 | if os_name.start_with? 'Windows' 48 | os_name = 'Windows' 49 | end 50 | return os_name 51 | end 52 | 53 | configure :os_arch => ['OS_NAME', 'OS_ARCH', 'ARCH_FLAVOR'] 54 | 55 | desc 'determining operating system' 56 | configure 'OS_NAME' => 'FOUND_JAVA' do 57 | os_name = detect_os 58 | CONFIG['OS_NAME'] = os_name.gsub(' ', '\ ') 59 | CONFIG.add_xml "" 60 | ok(os_name) 61 | end 62 | 63 | desc 'determining architecture' 64 | configure 'OS_ARCH' => 'FOUND_JAVA' do 65 | os_arch = %x(java -cp config PrintProperty os.arch).chomp 66 | CONFIG['OS_ARCH'] = os_arch 67 | CONFIG.add_xml "" 68 | ok(os_arch) 69 | end 70 | 71 | desc 'determining architecture flavor' 72 | configure 'ARCH_FLAVOR' => 'OS_ARCH' do 73 | if $opts.defined? :arch_flavor 74 | arch_flavor = $opts[:arch_flavor] 75 | elsif %w(i386 amd64 x86 x86_64).include? CONFIG['OS_ARCH'] 76 | Path.check_cmd('gcc') 77 | out = %x(gcc -o config/arch_flavor config/arch_flavor.c) 78 | fail('couldn\'t compile the config script') unless out.empty? 79 | arch_flavor = %x(config/arch_flavor).chomp 80 | else 81 | arch_flavor = '' 82 | end 83 | 84 | if arch_flavor.empty? or arch_flavor == 'none' 85 | CONFIG['OS_ARCH_WITH_FLAVOR'] = CONFIG['OS_ARCH'] 86 | else 87 | CONFIG['OS_ARCH_WITH_FLAVOR'] = CONFIG['OS_ARCH'] + File::SEPARATOR + arch_flavor 88 | end 89 | 90 | ok(arch_flavor) 91 | end 92 | 93 | if __FILE__ == $0 94 | ConfigureTask.run :os_arch 95 | end 96 | -------------------------------------------------------------------------------- /config/config_tools.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | require 'config/config' 36 | require 'config/path' 37 | require 'config/config_os_arch' 38 | require 'config/windows' 39 | 40 | include JblasConfig 41 | include Path 42 | 43 | configure :tools => ['FOUND_NM', 'FOUND_CYGPATH'] 44 | 45 | desc 'looking for nm' 46 | configure 'FOUND_NM' => ['OS_NAME', 'OS_ARCH'] do 47 | if CONFIG['OS_NAME'] == "Windows" and CONFIG['OS_ARCH'] == 'amd64' 48 | puts "Looking for 64bit toolchain" 49 | check_cmd(W64_PREFIX + 'nm') 50 | CONFIG['NM'] = W64_PREFIX + 'nm' 51 | else 52 | check_cmd 'nm' 53 | CONFIG['NM'] = 'nm' 54 | end 55 | CONFIG['FOUND_NM'] = true 56 | ok 57 | end 58 | 59 | configure 'FOUND_CYGPATH' => 'OS_NAME' do 60 | if CONFIG['OS_NAME'] == 'Windows' 61 | msg 'checking for cygpath' do 62 | check_cmd 'cygpath' 63 | CONFIG['FOUND_CGYPATH'] = true 64 | end 65 | end 66 | end 67 | 68 | if __FILE__ == $0 69 | ConfigureTask.run :tools 70 | end -------------------------------------------------------------------------------- /config/configure.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | $: << "." 36 | 37 | require 'config/path' 38 | require 'config/config' 39 | require 'config/opts' 40 | 41 | require 'config/config_tools' 42 | require 'config/config_os_arch' 43 | require 'config/config_cc' 44 | require 'config/config_fortran' 45 | require 'config/config_make' 46 | require 'config/config_lapack_sources' 47 | require 'config/config_libs' 48 | 49 | include JblasConfig 50 | include Path 51 | 52 | args = [] 53 | paste = false 54 | ARGV.each do |arg| 55 | if arg[-1] == ?\\ 56 | paste_next = true 57 | arg = arg[0...-1] + ' ' 58 | else 59 | paste_next = false 60 | end 61 | 62 | if paste 63 | args[-1] += arg 64 | else 65 | args << arg 66 | end 67 | 68 | paste = paste_next 69 | end 70 | 71 | $opts = Opts.new(args, {}, < [:os_arch, :tools, :java, :cc, :fortran, :make, :lapack_sources, :libs] 102 | 103 | run :all 104 | 105 | puts 106 | puts 'Configuration successful, writing out results to configure.out' 107 | open('configure.out', 'w') {|f| CONFIG.dump f} 108 | open('configure.xml', 'w') {|f| CONFIG.dump_xml f} 109 | -------------------------------------------------------------------------------- /config/opts.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | class Opts 36 | attr_accessor :opts, :args 37 | 38 | def initialize(args, shortcuts={}, usage=nil) 39 | @opts = Hash.new 40 | @args = Array.new 41 | args.each do |a| 42 | case a 43 | when '-h' 44 | puts usage 45 | exit 46 | when '--help' 47 | puts usage 48 | exit 49 | when /\A--/ 50 | a = a[2..-1] 51 | if a.index('=') 52 | key, value = a.split('=') 53 | opts[key.gsub(/-/, '_').to_sym] = value 54 | else 55 | opts[a.gsub(/-/, '_').to_sym] = true 56 | end 57 | when /\A-/ 58 | key = a[1..1].to_sym 59 | key = shortcuts[key] if shortcuts.has_key? key 60 | value = a[2..-1] 61 | if not value.empty? 62 | opts[key] = value 63 | else 64 | opts[key] = true 65 | end 66 | else 67 | @args << a 68 | end 69 | end 70 | 71 | def defined?(v) 72 | @opts.has_key? v 73 | end 74 | 75 | def [](key) 76 | @opts[key] 77 | end 78 | 79 | def get(key, default) 80 | if @opts.include? key 81 | @opts[key] 82 | else 83 | default 84 | end 85 | end 86 | 87 | def check?(admissible) 88 | unsupported = @opts.keys - admissible 89 | unless unsupported.empty? 90 | puts "Unsupported flags #{unsupported.join(', ')}" 91 | return false 92 | end 93 | true 94 | end 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /config/path.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | require 'set' 36 | require 'config/config' 37 | 38 | module Path 39 | PATH = ENV['PATH'].split(':') 40 | 41 | module_function 42 | 43 | def where(file, path=PATH) 44 | path.each do |p| 45 | fn = File.join(p, file) 46 | #print " Checking #{fn}" 47 | if File.exist? fn 48 | #puts " found" 49 | if block_given? 50 | return p if yield fn 51 | else 52 | return p 53 | end 54 | else 55 | #puts 56 | end 57 | end 58 | return 59 | end 60 | 61 | # returns the path to the command as specified by 62 | # line or nil if the command does not exist, or 63 | # it did not produce the right result 64 | def where_with_output(line, output) 65 | cmd = line.split[0] 66 | p = where(cmd) 67 | return unless p 68 | out = %x(#{File.join(p,line)}) 69 | if out =~ output 70 | return p 71 | else 72 | return 73 | end 74 | end 75 | 76 | # Check whether a cmd could be found. 77 | def check_cmd(*cmds) 78 | cmds.each do |cmd| 79 | JblasConfig.log "Searching for command #{cmd}" 80 | JblasConfig.fail("coulnd't find command #{cmd}") unless Path.where cmd 81 | end 82 | yield self if block_given? 83 | return 84 | end 85 | 86 | # Check whether files could be found in the given path. 87 | def check_files(path, *files) 88 | files.each do |file| 89 | file = File.join(path, *file) 90 | JblasConfig.log "Searching for file #{file}" 91 | JblasConfig.fail("couldn't find #{file}") unless File.exist? file 92 | end 93 | yield if block_given? 94 | return 95 | end 96 | 97 | # translate dir (mainly necessary for cygwin) 98 | def dir(s) 99 | case JblasConfig::CONFIG['OS_NAME'] 100 | when 'Windows' 101 | s = s.gsub(/\\/, '\\\\\\\\') 102 | %x(cygpath -u '#{s}').chomp 103 | else 104 | s # safe default... 105 | end 106 | end 107 | end -------------------------------------------------------------------------------- /config/string_ext.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | # indent a multiline text 36 | class String # :nodoc: 37 | def indent(cnt) 38 | split("\n").map {|l| ' ' * cnt + l}.join("\n") 39 | end 40 | 41 | def start_with?(head) 42 | self.length > head.length && self[0...head.length] == head 43 | end 44 | 45 | def escape 46 | gsub /\s/, '\ ' 47 | end 48 | end -------------------------------------------------------------------------------- /config/windows.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | require 'config/config' 36 | 37 | W64_PREFIX = 'x86_64-w64-mingw32-' 38 | W32_PREFIX = 'i686-pc-mingw32-' 39 | 40 | def w64build? 41 | JblasConfig::CONFIG['OS_NAME'] == 'Windows' and JblasConfig::CONFIG['OS_ARCH'] == 'amd64' 42 | end 43 | -------------------------------------------------------------------------------- /configure: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ "$1" == "--keep-options" ]; then 4 | shift 5 | else 6 | echo $* >configure.options 7 | fi 8 | 9 | ruby config/configure.rb $* 10 | 11 | -------------------------------------------------------------------------------- /docker/dev-ubuntu1804-arm64.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 18.04 2 | 3 | FROM ubuntu:18.04 4 | 5 | RUN apt-get update 6 | RUN apt-get -y install less htop build-essential vim ruby 7 | RUN apt-get -y install openjdk-8-jdk-headless maven ant 8 | RUN apt-get -y install gfortran libopenblas-dev 9 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 10 | 11 | CMD mkdir /home/dev 12 | WORKDIR /home/dev -------------------------------------------------------------------------------- /docker/dev-ubuntu1804.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 18.04 2 | 3 | FROM ubuntu:18.04 4 | 5 | RUN apt-get update 6 | RUN apt-get -y install less htop build-essential vim ruby 7 | RUN apt-get -y install openjdk-8-jdk-headless maven ant 8 | RUN apt-get -y install gfortran libopenblas-dev 9 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 10 | 11 | CMD mkdir /home/dev 12 | WORKDIR /home/dev -------------------------------------------------------------------------------- /docker/dev-ubuntu2004-arm64.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 20.04 2 | 3 | FROM ubuntu:20.04 4 | 5 | RUN apt-get update 6 | RUN apt-get -y install less htop build-essential vim ruby 7 | RUN apt-get -y install openjdk-8-jdk-headless maven ant 8 | RUN apt-get -y install gfortran libopenblas-serial-dev 9 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 10 | 11 | CMD mkdir /home/dev 12 | WORKDIR /home/dev -------------------------------------------------------------------------------- /docker/dev-ubuntu2004.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 20.04 2 | 3 | FROM ubuntu:20.04 4 | 5 | RUN apt-get update 6 | RUN apt-get -y install less htop build-essential vim ruby 7 | RUN apt-get -y install openjdk-8-jdk-headless maven ant 8 | RUN apt-get -y install gfortran libopenblas-serial-dev 9 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 10 | 11 | CMD mkdir /home/dev 12 | WORKDIR /home/dev -------------------------------------------------------------------------------- /docker/test-ubuntu1804-arm64.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 20.04 2 | 3 | # run as docker build -f test-ubuntu2004.Dockerfile . 4 | 5 | FROM ubuntu:18.04 6 | 7 | ENV JBLAS_VERSION=1.2.5-SNAPSHOT 8 | 9 | RUN apt-get update 10 | RUN apt-get -y install openjdk-8-jdk-headless 11 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 12 | 13 | RUN mkdir /home/dev 14 | WORKDIR /home/dev 15 | 16 | COPY target/jblas-${JBLAS_VERSION}.jar . 17 | 18 | RUN java -jar jblas-${JBLAS_VERSION}.jar -------------------------------------------------------------------------------- /docker/test-ubuntu1804.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 20.04 2 | 3 | # run as docker build -f test-ubuntu2004.Dockerfile . 4 | 5 | FROM ubuntu:18.04 6 | 7 | ENV JBLAS_VERSION=1.2.5-SNAPSHOT 8 | 9 | RUN apt-get update 10 | RUN apt-get -y install openjdk-8-jdk-headless 11 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 12 | 13 | RUN mkdir /home/dev 14 | WORKDIR /home/dev 15 | 16 | COPY target/jblas-${JBLAS_VERSION}.jar . 17 | 18 | RUN java -jar jblas-${JBLAS_VERSION}.jar -------------------------------------------------------------------------------- /docker/test-ubuntu2004-arm64.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 20.04 2 | 3 | # run as docker build -f test-ubuntu2004.Dockerfile . 4 | 5 | FROM ubuntu:20.04 6 | 7 | ENV JBLAS_VERSION=1.2.5-SNAPSHOT 8 | 9 | RUN apt-get update 10 | RUN apt-get -y install openjdk-8-jdk-headless 11 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-arm64/jre/bin/java 12 | 13 | RUN mkdir /home/dev 14 | WORKDIR /home/dev 15 | 16 | COPY target/jblas-${JBLAS_VERSION}.jar . 17 | 18 | RUN java -jar jblas-${JBLAS_VERSION}.jar -------------------------------------------------------------------------------- /docker/test-ubuntu2004.Dockerfile: -------------------------------------------------------------------------------- 1 | # Docker build environment for Ubuntu 20.04 2 | 3 | # run as docker build -f test-ubuntu2004.Dockerfile . 4 | 5 | FROM ubuntu:20.04 6 | 7 | ENV JBLAS_VERSION=1.2.5-SNAPSHOT 8 | 9 | RUN apt-get update 10 | RUN apt-get -y install openjdk-8-jdk-headless 11 | RUN update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 12 | 13 | RUN mkdir /home/dev 14 | WORKDIR /home/dev 15 | 16 | COPY target/jblas-${JBLAS_VERSION}.jar . 17 | 18 | RUN java -jar jblas-${JBLAS_VERSION}.jar -------------------------------------------------------------------------------- /docs/jblas-logo-64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/docs/jblas-logo-64.png -------------------------------------------------------------------------------- /docs/jblas-logo-square.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/docs/jblas-logo-square.png -------------------------------------------------------------------------------- /docs/jblas-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/docs/jblas-logo.png -------------------------------------------------------------------------------- /docs/jblas-thumb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/docs/jblas-thumb.png -------------------------------------------------------------------------------- /docs/render.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'redcloth' 3 | 4 | ARGV.each do |f| 5 | newf = f.gsub /.textile$/, '.html' 6 | out = open(newf, 'w') do |o| 7 | txt = open(f, 'r').read 8 | o.write RedCloth.new(txt).to_html 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /examples/complex.rb: -------------------------------------------------------------------------------- 1 | # This file demonstrates how to access a java file contained in a 2 | # jar file, how to extend the java object and make a numeric 3 | # class interact seamlessly with normal ruby numeric objects 4 | 5 | # load the jar file 6 | require '../jBLAS.jar' 7 | 8 | # We provide a short-cut for the class com.fhg.core.DoubleComplex 9 | DoubleComplex = Java::edu.ida.core.DoubleComplex 10 | 11 | # we extend the class 12 | class DoubleComplex 13 | # add operators 14 | def +(o); add(promote(o)); end 15 | def -(o); sub(promote(o)); end 16 | def *(o); mul(promote(o)); end 17 | def /(o); div(promote(o)); end 18 | 19 | # extend the range of the operators to also deal with 20 | # non-complex numeric arguments 21 | def promote(o) 22 | if Numeric === o 23 | DoubleComplex.new(o, 0) 24 | else 25 | o 26 | end 27 | end 28 | 29 | # make sure that it also works when the left hand side operator 30 | # is not a double 31 | def coerce(o) 32 | unless DoubleComplex === o 33 | [self, o] 34 | end 35 | end 36 | 37 | # to make sure that we look pretty in irb 38 | def inspect 39 | toString 40 | end 41 | end 42 | 43 | # imaginary number 44 | I = DoubleComplex.new(0, 1) 45 | 46 | # and voila, it works! 47 | puts (1 + 3*I)/ 2*I 48 | -------------------------------------------------------------------------------- /examples/complex_svd.rb: -------------------------------------------------------------------------------- 1 | require 'java' 2 | require 'jblas-1.0.3.jar' 3 | 4 | java_import 'org.jblas.ComplexDoubleMatrix' 5 | java_import 'org.jblas.DoubleMatrix' 6 | java_import 'org.jblas.Singular' 7 | 8 | Matrix = ComplexDoubleMatrix 9 | 10 | M = 4 11 | N = 3 12 | 13 | m = Matrix.new(M, N) 14 | 15 | for i in 0...M 16 | for j in 0...N 17 | m.put(i, j, i+j, i*j) 18 | #m.put(i, j, i+j) 19 | end 20 | end 21 | 22 | puts m.dup 23 | 24 | u, s, v = Singular.sparseSVD(m) 25 | 26 | s = Matrix.diag(s) 27 | 28 | puts "u = #{u}" 29 | puts "s = #{s}" 30 | puts "v = #{v}" 31 | 32 | puts u.mmul(s).mmul(v.transpose).sub(m).norm2() 33 | 34 | puts Singular.SVDValues(m) 35 | -------------------------------------------------------------------------------- /examples/jmatrix.rb: -------------------------------------------------------------------------------- 1 | # This file contains a very unfinished sketch of how you can use the matrix 2 | # classes from jruby. Note that we make heavy use of ruby's ability to extend 3 | # existing classes, for example, in order to devine arithmetic operators for 4 | # the matrix classes. 5 | require 'java' 6 | require '../jBLAS.jar' 7 | 8 | module JBLAS 9 | include Java 10 | import 'edu.ida.la.DoubleMatrix' 11 | import 'edu.ida.la.FloatMatrix' 12 | import 'edu.ida.la.DoubleVector' 13 | import 'edu.ida.la.FloatVector' 14 | import 'edu.ida.la.SimpleBlas' 15 | import 'edu.ida.la.DoubleFunction' 16 | import 'edu.ida.la.FloatFunction' 17 | 18 | import 'edu.ida.la.Solve' 19 | import 'edu.ida.la.Eigen' 20 | import 'edu.ida.la.Geometry' 21 | 22 | # This module contains the matrix object extensions which will later be mixed-in 23 | # to DoubleMatrix and FloatMatrix. This safes us from unnecessary code 24 | # duplication. 25 | module MatrixExt 26 | def [](i, j) 27 | get(i, j) 28 | end 29 | 30 | def []=(i, j, v) 31 | put(i, j, v) 32 | end 33 | 34 | def +(o); add(o); end 35 | def -(o); sub(o); end 36 | def *(o); mul(o); end 37 | 38 | def coerce(o) 39 | unless self.class === o 40 | [self, o] 41 | end 42 | end 43 | 44 | def t; transpose; end 45 | end 46 | 47 | module MatrixStaticExt 48 | def [](*data) 49 | n = data.length 50 | if data.reject{|l| Numeric === l}.size == 0 51 | a = self.new(1, n) 52 | (0...data.length).each do |i| 53 | a[0, i] = data[i] 54 | end 55 | return a 56 | else 57 | begin 58 | lengths = data.collect{|v| v.length} 59 | rescue 60 | raise "All columns must be arrays" 61 | end 62 | raise "All columns must have equal length!" if lengths.min < lengths.max 63 | a = self.new(n, lengths.max) 64 | for i in 0...n 65 | for j in 0...lengths.max 66 | a[i,j] = data[i][j] 67 | end 68 | end 69 | return a 70 | end 71 | end 72 | end 73 | 74 | # Extend the matrix classes with the functionalities defined above. 75 | class DoubleMatrix 76 | class << self 77 | include MatrixStaticExt 78 | end 79 | include MatrixExt 80 | end 81 | 82 | class FloatMatrix 83 | class << self 84 | include MatrixStaticExt 85 | end 86 | include MatrixExt 87 | end 88 | 89 | # Double Vector and Float Vector, fully spelled out this time. 90 | class DoubleVector 91 | def [](i); get(i); end 92 | 93 | def []=(i, v); put(i, v); end 94 | 95 | def +(o); add(o); end 96 | def -(o); sub(o); end 97 | def *(o); mul(o); end 98 | 99 | def self.[](*data) 100 | a = self.new(data.length) 101 | for i in (0...data.length) 102 | a[i] = data[i].to_f 103 | end 104 | return a 105 | end 106 | 107 | def coerce(o) 108 | unless self.class === o 109 | [self, o] 110 | end 111 | end 112 | 113 | def to_s 114 | "[" + toString.gsub(/\t/, ', ') + "]" 115 | end 116 | 117 | class Fct 118 | include DoubleFunction 119 | 120 | def initialize(block) 121 | @block = block 122 | end 123 | 124 | def compute(x) 125 | @block.call(x) 126 | end 127 | end 128 | 129 | alias java_map map 130 | 131 | # a slightly more rubyesque map function which takes a block 132 | def map(&block) 133 | java_map(Fct.new block) 134 | end 135 | end 136 | 137 | class FloatVector 138 | def [](i); get(i); end 139 | 140 | def []=(i, v); put(i, v); end 141 | 142 | def +(o); add(o); end 143 | def -(o); sub(o); end 144 | def *(o); mul(o); end 145 | 146 | def self.[](*data) 147 | a = self.new(data.length) 148 | for i in (0...data.length) 149 | a[i] = data[i].to_f 150 | end 151 | return a 152 | end 153 | 154 | def coerce(o) 155 | unless self.class === o 156 | [self, o] 157 | end 158 | end 159 | 160 | def to_s 161 | "[" + toString.gsub(/\t/, ', ') + "]" 162 | end 163 | 164 | class Fct 165 | include FloatFunction 166 | 167 | def initialize(block) 168 | @block = block 169 | end 170 | 171 | def compute(x) 172 | @block.call(x) 173 | end 174 | end 175 | 176 | alias java_map map 177 | 178 | # a slightly more rubyesque map function which takes a block 179 | def map(&block) 180 | java_map(Fct.new block) 181 | end 182 | end 183 | end 184 | -------------------------------------------------------------------------------- /examples/test_abitofml.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'jmatrix' 3 | require 'tictoc' 4 | 5 | include JBLAS 6 | 7 | import 'edu.ida.la.Solve' 8 | import 'edu.ida.la.Eigen' 9 | import 'edu.ida.la.Geometry' 10 | 11 | # 12 | # Radial Basis Function Kernel 13 | # 14 | class RbfKern 15 | def initialize(width) 16 | @width = width 17 | end 18 | 19 | def [](x, y=nil) 20 | #puts "upsi" 21 | if not y 22 | y = x 23 | end 24 | 25 | Geometry.pairwiseSquaredDistances(x, y).muli(-1.0/(2.0*@width)).expi() 26 | end 27 | end 28 | 29 | def normalize_mse(yhat, y) 30 | (y - yhat).norm2 / (y - y.mean).norm2 31 | end 32 | 33 | class KRR 34 | def initialize(kernel) 35 | @kernel = kernel 36 | end 37 | 38 | def train(tau, x, y) 39 | n = y.length 40 | tic "Setting up kernel matrix" 41 | k = @kernel[x] 42 | toc 43 | tic "Computing alpha (sysv)" 44 | @alpha = Solve.solveSymmetric(k + tau * k.class.eye(n), y) 45 | toc 46 | tic "Computing alpha (posv)" 47 | @alpha = Solve.solvePositive(k + tau * k.class.eye(n), y) 48 | toc 49 | 50 | tic "computing eigenvalues" 51 | ev = Eigen.symmetricEigenvalues(k) 52 | toc 53 | 54 | tic "computing eigenvectors and eigenvalues" 55 | ev = Eigen.symmetricEigenvectors(k) 56 | toc 57 | 58 | puts ev 59 | if n < 50 60 | puts ev[0] 61 | puts 62 | puts ev[1] 63 | end 64 | 65 | @trainX = x 66 | end 67 | 68 | def [](x) 69 | tic "Setting up kernel matrix for prediction" 70 | k = @kernel[x, @trainX] 71 | toc 72 | tic "Predicting" 73 | yhat = k * @alpha 74 | toc 75 | return yhat 76 | end 77 | end 78 | 79 | 80 | class ABitOfML < Test::Unit::TestCase 81 | def krr_on_sinc(n, vec, ext) 82 | 83 | tic "Generating the data..." 84 | #x = 8 * vec.rand(n).sort - 4 85 | x = 8 * vec.rand(n) - 4 86 | s = x.sinc() 87 | y = s + vec.randn(n) * 0.1 88 | toc 89 | 90 | krr = KRR.new(RbfKern.new 1.0) 91 | krr.train(1e-3, x, y) 92 | yhat = krr[x] 93 | 94 | fit_error = normalize_mse(yhat, y) 95 | 96 | puts "Fit error #{fit_error}" 97 | 98 | assert_in_delta(0.3, fit_error, 0.1) 99 | 100 | # 101 | # generate a matlab script to plot the result 102 | # 103 | open("sincdemo_#{ext}.m", "w") do |o| 104 | o.write "X = #{x};" 105 | o.write "Y = #{y};" 106 | o.write "Yhat = #{yhat};" 107 | o.write "plot(X, Y, '.', X, Yhat, 'r-', 'LineWidth', 5);" 108 | end 109 | end 110 | 111 | def test_double 112 | krr_on_sinc(1000, DoubleVector, 'double') 113 | end 114 | 115 | def test_float 116 | krr_on_sinc(1000, FloatVector, 'float') 117 | end 118 | end 119 | -------------------------------------------------------------------------------- /examples/test_matrix.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'jmatrix.rb' 3 | require 'tictoc.rb' 4 | 5 | include JBLAS 6 | 7 | class TestMatrix < Test::Unit::TestCase 8 | def run_some_tests(mat,vec) 9 | # construct 3 * 3 matrix and fill in some values 10 | a = mat.new(3,3) 11 | for i in 0..2 12 | for j in 0..2 13 | a[i,j] = i + j 14 | end 15 | end 16 | 17 | puts a 18 | 19 | # construct a matrix from nested arrays 20 | x = mat[[1], [2], [3]] 21 | a = mat[[1, 2, 3], [4, 5, 6], [7, 8, 9]] 22 | 23 | puts x 24 | puts a 25 | 26 | # normal arithmetics work 27 | puts (y = a * vec[1, 2, 3]) 28 | 29 | # elementwise exponential 30 | puts vec[1,2,3].exp 31 | 32 | 33 | # elementwise exponential on a long vector 34 | z = vec[*(0..10000).to_a] 35 | tic "applying exp to the whole matrix" 36 | x = z.dup.map {|x| Math.exp(x)} 37 | toc 38 | 39 | tic "the same in Java" 40 | x = z.dup.exp 41 | toc 42 | 43 | # computing norms 44 | puts "y = #{y}" 45 | puts "2-norm of y = #{y.norm2}" 46 | puts "1-norm of y = #{y.norm1}" 47 | puts "maximum norm of y = #{y.maxnorm}" 48 | 49 | puts "converting a vector to an array" 50 | puts y.to_array 51 | puts a.to_array 52 | 53 | # a big matrix 54 | n = 1000 55 | puts "filling #{n} * #{n} matrix" 56 | m = mat.new(n, n) 57 | for i in 0...10 58 | for j in 0...n 59 | m[i,j] = rand 60 | end 61 | end 62 | 63 | puts "Multiplying with itself" 64 | 65 | 5.times do 66 | tic "Timing" 67 | c = m * m 68 | toc 69 | end 70 | 71 | # rank one update 72 | x = vec.rand(1000) 73 | 74 | 5.times do 75 | tic "Rank one update with xGER " 76 | c = m.rank_one_update(x) 77 | toc 78 | tic "using explicit matrix operations" 79 | c = m + x.as_column_vector * x.as_row_vector 80 | toc 81 | end 82 | 83 | tic "compared to matrix - vector multiplication" 84 | c = m * x 85 | toc 86 | 87 | end 88 | 89 | def test_double 90 | run_some_tests(DoubleMatrix, DoubleVector) 91 | end 92 | 93 | def test_float 94 | run_some_tests(FloatMatrix, FloatVector) 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /examples/test_time_eigen.rb: -------------------------------------------------------------------------------- 1 | require 'tictoc' 2 | require 'jmatrix' 3 | require 'benchmark' 4 | 5 | include JBLAS 6 | 7 | def char(s) 8 | case s 9 | when String 10 | java.lang.Character.new(s[0]) 11 | when Fixnum 12 | java.lang.Character.new(s) 13 | end 14 | end 15 | 16 | n = 1000 17 | 18 | def time_it(n, i) 19 | x = DoubleVector.randn(n).sort 20 | k = Geometry.pairwiseSquaredDistances(x, x).muli(-2).expi 21 | ev = DoubleVector.new(n) 22 | 23 | puts "Benchmark for n = #{n} (reported numbers are #{i} iterations)" 24 | 25 | Benchmark.benchmark do |r| 26 | r.report("syev eigenvalues: ") { i.times { SimpleBlas.syev(char('N'), char('U'), k.dup, ev) } } 27 | r.report("syevd eigenvalues: ") { i.times { SimpleBlas.syevd(char('N'), char('U'), k.dup, ev) } } 28 | r.report("syev eigenvectors: ") { i.times { SimpleBlas.syev(char('V'), char('U'), k.dup, ev) } } 29 | r.report("syevd eigenvectors:") { i.times { SimpleBlas.syevd(char('V'), char('U'), k.dup, ev) } } 30 | end 31 | end 32 | 33 | time_it(100, 100) 34 | time_it(500, 20) 35 | time_it(1000, 5) -------------------------------------------------------------------------------- /examples/tictoc.rb: -------------------------------------------------------------------------------- 1 | # some tools for timing code 2 | # 3 | # Either used as 4 | # 5 | # tic 6 | # do something... 7 | # tic 8 | # 9 | # or as a block 10 | # 11 | # tictoc do 12 | # do something... 13 | # end 14 | # 15 | # The non-block one might be more handy if you define new variables in your code. 16 | 17 | def tic(message=nil) 18 | if message 19 | print message + "... " 20 | $stdout.flush 21 | end 22 | $saved_time = Time.new 23 | end 24 | 25 | def toc 26 | print "(#{Time.new - $saved_time} seconds)\n" 27 | end 28 | 29 | def tictoc(message=nil) 30 | tic message 31 | yield 32 | toc 33 | end 34 | -------------------------------------------------------------------------------- /fortranwrapper.dump: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/fortranwrapper.dump -------------------------------------------------------------------------------- /scripts/c-file.c: -------------------------------------------------------------------------------- 1 | /// --- BEGIN LICENSE BLOCK --- 2 | // Copyright (c) 2009, Mikio L. Braun 3 | // All rights reserved. 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following 14 | // disclaimer in the documentation and/or other materials provided 15 | // with the distribution. 16 | // 17 | // * Neither the name of the Technische Universitaet Berlin nor the 18 | // names of its contributors may be used to endorse or promote 19 | // products derived from this software without specific prior 20 | // written permission. 21 | // 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | /// --- END LICENSE BLOCK --- 34 | 35 | /* 36 | * Wrapper files for LAPACK 37 | * 38 | * 39 | * automatically generated by fortranwrapper 40 | */ 41 | 42 | #include "<%= filename %>.h" 43 | #include 44 | #include 45 | 46 | <% for r in routines %> 47 | <%= C.wrapper_for r -%> 48 | <% end%> 49 | -------------------------------------------------------------------------------- /scripts/c-header.h: -------------------------------------------------------------------------------- 1 | /// --- BEGIN LICENSE BLOCK --- 2 | // Copyright (c) 2009, Mikio L. Braun 3 | // All rights reserved. 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions are 7 | // met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // 12 | // * Redistributions in binary form must reproduce the above 13 | // copyright notice, this list of conditions and the following 14 | // disclaimer in the documentation and/or other materials provided 15 | // with the distribution. 16 | // 17 | // * Neither the name of the Technische Universitaet Berlin nor the 18 | // names of its contributors may be used to endorse or promote 19 | // products derived from this software without specific prior 20 | // written permission. 21 | // 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | /// --- END LICENSE BLOCK --- 34 | 35 | /* 36 | * Wrapper files for FORTRAN 37 | * 38 | * automatically generated by fortranwrapper 39 | */ 40 | 41 | #ifndef <%= filename.upcase %>_H 42 | #define <%= filename.upcase %>_H 43 | 44 | typedef struct { float r; float i; } <%= C::COMPLEX_TYPE %>; 45 | typedef struct { double r; double i; } <%= C::DOUBLE_COMPLEX_TYPE %>; 46 | 47 | <% routines.each do |r| -%> 48 | extern <%= C.declaration_for r %>; 49 | <% end -%> 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /scripts/class_to_float.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | $CRLF = false 36 | 37 | def readfile(fn) 38 | f = open(fn) 39 | s = f.read 40 | f.close 41 | if s.index("\r\n") 42 | $CRLF = true 43 | puts "#$0: Detected CRLF instead of LF" 44 | s.gsub! /\r\n/, "\n" 45 | end 46 | s 47 | end 48 | 49 | def writefile(fn, s) 50 | if $CRLF 51 | puts "#$0: Translating LF back to CRLF" 52 | s.gsub! /\n/, "\r\n" 53 | end 54 | f = open(fn, 'w') 55 | f.write s 56 | f.close 57 | end 58 | 59 | def classfile(classname) 60 | classname.tr('.', File::SEPARATOR) + '.java' 61 | end 62 | 63 | def translate(s) 64 | s.gsub! /DoubleMatrix/, 'FloatMatrix' 65 | s.gsub! /DoubleBuffer/, 'FloatBuffer' 66 | s.gsub! /DoubleVector/, 'FloatVector' 67 | s.gsub! /ComplexDouble/, 'ComplexFloat' 68 | s.gsub! /([0-9]+\.[0-9]+(?:[eE]-?[0-9]+)?)/, '\1f' 69 | s.gsub! /double/, 'float' 70 | s.gsub! /(\s)NativeBlas\.dz/, '\1NativeBlas.sc' 71 | s.gsub! /(\s)NativeBlas\.d/, '\1NativeBlas.s' 72 | s.gsub! /(\s)NativeBlas\.z/, '\1NativeBlas.c' 73 | s.gsub! /nextDouble/, 'nextFloat' 74 | s.gsub! /DoubleFunction/, 'FloatFunction' 75 | s.gsub! /TestBlasDouble/, 'TestBlasFloat' 76 | s.gsub! /Double\./, 'Float.' 77 | s.gsub! /(\s)Double(\s)/, '\1Float\1' 78 | s.gsub! //, '' 79 | s.gsub! /readDouble/, 'readFloat' 80 | s.gsub! /java.lang.Double/, 'java.lang.Float' 81 | s.gsub! /writeDouble/, 'writeFloat' 82 | 83 | # go through lines. If a line starts with "//FLOAT//" replace the next line by the following line 84 | discard_lines = 0 85 | result = [] 86 | s.split("\n").each do |line| 87 | if discard_lines > 0 88 | discard_lines -= 1 89 | else 90 | i = line.index('//FLOAT//') 91 | if i 92 | discard_lines = 1 93 | result <<= line[(i + "//FLOAT//".length)..-1] 94 | else 95 | result <<= line 96 | end 97 | end 98 | end 99 | 100 | result.join("\n") 101 | end 102 | 103 | if ARGV.size < 2 104 | puts "Usage: ruby class_to_float.rb srcprefix classname" 105 | exit 106 | end 107 | 108 | prefix = ARGV[0] 109 | source = ARGV[1] 110 | target = source.gsub(/Double/, 'Float') 111 | 112 | sourcefn = File.join(prefix, classfile(source)) 113 | targetfn = File.join(prefix, classfile(target)) 114 | 115 | if File.exist? sourcefn 116 | s = readfile(sourcefn) 117 | writefile(targetfn, translate(s)) 118 | else 119 | puts "Could not read file #{sourcefn}" 120 | end 121 | -------------------------------------------------------------------------------- /scripts/java-class.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | package <%= package %>; 37 | 38 | import org.jblas.util.Logger; 39 | 40 | /** 41 | * Native BLAS and LAPACK functions. 42 | * 43 | *

The <%= classname %> class contains the native BLAS and LAPACK functions. Each 44 | * Fortran function is mapped to a static method of this class. For each array argument, 45 | * an additional parameter is introduced which gives the offset from the beginning of 46 | * the passed array. In C, you would be able to pass a different pointer, but 47 | * in Java, you can only pass the whole array.

48 | * 49 | *

Note that due to the way the JNI is usually implemented, the arrays are first 50 | * copied outside of the JVM before the function is called. This means that 51 | * functions whose runtime is linear in the amount of memory do usually not 52 | * run faster just because you are using a native implementation. This holds true 53 | * for most Level 1 BLAS routines (like vector addition), and unfortunately also 54 | * for most Level 2 BLAS routines (like matrix-vector multiplication). For these, 55 | * there exists a class JavaBlas which contains Java implementations.

56 | * 57 | *

In LAPACK, there exist routines which require workspace to be allocated together 58 | * with a standard procedure for computing the size of these workspaces. jblas 59 | * automatically also generates wrappers for these routines with automatic 60 | * workspace allocation. These routines have the same name, but the workspace 61 | * arguments are removed.

62 | * 63 | *

Finally, an example: The fortran routine

64 |  * SUBROUTINE DAXPY(N,DA,DX,INCX,DY,INCY)
65 |  *     DOUBLE PRECISION DA
66 |  *     INTEGER INCX,INCY,N
67 |  *     DOUBLE PRECISION DX(*),DY(*)
68 |  * 
69 | * becomes
70 |  * public static native void daxpy(int n, double da, double[] dx, int dxIdx,
71 |  *    int incx, double[] dy, int dyIdx, int incy);
72 |  * 
73 | */ 74 | public class <%= classname %> { 75 | 76 | static { 77 | NativeBlasLibraryLoader.loadLibraryAndCheckErrors(); 78 | } 79 | 80 | private static int[] intDummy = new int[1]; 81 | private static double[] doubleDummy = new double[1]; 82 | private static float[] floatDummy = new float[1]; 83 | 84 | <% for r in routines -%> 85 | <%= generate_native_declaration r %> 86 | <% end %> 87 | } 88 | -------------------------------------------------------------------------------- /scripts/render-textile.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | require 'set' 36 | 37 | require 'rubygems' 38 | require 'redcloth' 39 | 40 | def link_to_package(txt, pkg) 41 | txt.gsub!(pkg, 42 | "#{pkg}") 43 | end 44 | 45 | def link_to_class(txt, cls) 46 | fullname = SOURCES. 47 | grep(Regexp.new(cls))[0]. 48 | tr('.', '/'). 49 | gsub('/java', '.html'). 50 | gsub('src/', '') 51 | txt.gsub!(/(\W)#{cls}(\W)/) do |m| 52 | $1 + "#{cls}" + $2 53 | end 54 | end 55 | 56 | SOURCES = Dir.glob('src/**/*.java') 57 | CLASSES = SOURCES.map {|f| File.basename(f, '.java')} 58 | PACKAGES = Set.new(SOURCES.map {|f| File.dirname(f)}) 59 | 60 | #puts "Classes:" 61 | #CLASSES.each {|c| puts c} 62 | #puts 63 | #puts "Packages:" 64 | #PACKAGES.each {|p| puts p} 65 | 66 | #exit 67 | 68 | ARGV.each do |f| 69 | newf = f.gsub /.textile$/, '.html' 70 | out = open(newf, 'w') do |o| 71 | txt = open(f, 'r').read 72 | html = RedCloth.new(txt).to_html 73 | html.gsub!(/
/, '') 74 | html.gsub!(//, '
') 75 | PACKAGES.each {|p| link_to_package(html, p)} 76 | CLASSES.each {|c| link_to_class(html, c)} 77 | o.write('' + html + '') 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /scripts/rjpp.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | #!/usr/bin/ruby 36 | # 37 | # The Ruby Java PreProcessor 38 | # 39 | # Contrary to the "usual" cpp, commands will be expanded "in-place" in 40 | # an idempotent fashion (meaning that you can run rjpp on the same 41 | # file more than once.) 42 | # 43 | # Directives are either given as 44 | # 45 | # /*# ... directive ... #*/ 46 | # 47 | # After expansion, the code will look like this: 48 | # 49 | # /*# original directive #*/ 50 | # //RJPP-BEGIN------------------------------------------------------------ 51 | # expanded code 52 | # //RJPP-END-------------------------------------------------------------- 53 | # 54 | # So you can run the rjpp twice on a file and get the same result as 55 | # running it once. This property is called idempotency in mathematics X-D 56 | # 57 | # New feature: 58 | # 59 | # /* 60 | 61 | 62 | # print usage 63 | if ARGV.length == 0 64 | puts "Usage: rjpp file" 65 | end 66 | 67 | def collect(*args) 68 | args.join "\n" 69 | end 70 | 71 | def doc(s) 72 | "/** " + s + " */" 73 | end 74 | 75 | filename = ARGV[0] 76 | 77 | #open file 78 | file = open(filename, 'r').read 79 | saved_file = file 80 | 81 | # remove existing expansions 82 | file.gsub! /\/\/RJPP-BEGIN.*?\/\/RJPP-END[^\n]*\n/m, '' 83 | 84 | # expand code 85 | file.gsub! /\/\*\#(.*?)\#\*\//m do |s| 86 | result = s 87 | expansion = eval($1) 88 | if expansion 89 | if Array === expansion 90 | expansion = expansion.join 91 | end 92 | unless expansion.empty? or expansion.is_a? Symbol 93 | result << ("\n//RJPP-BEGIN------------------------------------------------------------\n" + expansion + "//RJPP-END--------------------------------------------------------------") 94 | end 95 | end 96 | result 97 | end 98 | 99 | # write file 100 | if file.length == 0 101 | puts "How odd... The whole file magically vanished..." 102 | else 103 | open(ARGV[0] + '.rjpp', 'w').write(saved_file) 104 | open(ARGV[0], 'w').write(file) 105 | #print file 106 | end 107 | -------------------------------------------------------------------------------- /scripts/static_class_to_float.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | $CRLF = false 36 | 37 | def readfile(fn) 38 | f = open(fn) 39 | s = f.read 40 | f.close 41 | if s.index("\r\n") 42 | $CRLF = true 43 | puts "#$0: Detected CRLF instead of LF" 44 | s.gsub! /\r\n/, "\n" 45 | end 46 | s 47 | end 48 | 49 | def writefile(fn, s) 50 | if $CRLF 51 | puts "#$0: Translating LF back to CRLF" 52 | s.gsub! /\n/, "\r\n" 53 | end 54 | f = open(fn, 'w') 55 | f.write s 56 | f.close 57 | end 58 | 59 | def classfile(classname) 60 | classname.tr('.', File::SEPARATOR) + '.java' 61 | end 62 | 63 | def translate(cl, s) 64 | mark = "public class #{cl.split('.').last} {\n" 65 | i = s.index(mark) + mark.length 66 | j = s.rindex('}') 67 | head = s[0...i] 68 | body = s[i...j] 69 | 70 | # remove part copied in last time 71 | body.gsub! /\/\/BEGIN.*?\/\/END\n/m, '' 72 | 73 | copybody = body.dup 74 | 75 | # remove part which should not be added as float 76 | copybody.gsub! /\/\/STOP.*?\/\/START\n/m, '' 77 | 78 | # do necessary substitutions 79 | copybody.gsub! /DoubleMatrix/, 'FloatMatrix' 80 | copybody.gsub! /DoubleBuffer/, 'FloatBuffer' 81 | copybody.gsub! /DoubleVector/, 'FloatVector' 82 | copybody.gsub! /ComplexDouble/, 'ComplexFloat' 83 | copybody.gsub! /([0-9]+\.[0-9]+(?:[eE]-?[0-9]+)?)/, '\1f' 84 | copybody.gsub! /double/, 'float' 85 | copybody.gsub! /NativeBlas\.dz/, 'NativeBlas.sc' 86 | copybody.gsub! /NativeBlas\.d/, 'NativeBlas.s' 87 | copybody.gsub! /NativeBlas\.z/, 'NativeBlas.c' 88 | copybody.gsub! /NativeBlas\.idamax/, 'NativeBlas.isamax' 89 | copybody.gsub! /NativeBlas\.izamax/, 'NativeBlas.icamax' 90 | copybody.gsub!(/([a-z]+)Double/, '\1Float') 91 | 92 | # construct output file 93 | out = [] 94 | out << head 95 | out << body 96 | out << "//BEGIN\n" 97 | out << " // The code below has been automatically generated.\n" 98 | out << " // DO NOT EDIT!\n" 99 | out << copybody 100 | out << "//END\n" 101 | out << "}\n" 102 | return out.join 103 | end 104 | 105 | if ARGV.size < 2 106 | puts "Usage: ruby static_class_to_float.rb srcprefix classname" 107 | exit 108 | end 109 | 110 | prefix = ARGV[0] 111 | source = ARGV[1] 112 | 113 | fn = File.join(prefix, classfile(source)) 114 | 115 | if File.exist? fn 116 | s = readfile(fn) 117 | writefile(fn, translate(source, s)) 118 | else 119 | puts "Could not read file #{fn}" 120 | end 121 | -------------------------------------------------------------------------------- /scripts/template_context.rb: -------------------------------------------------------------------------------- 1 | ## --- BEGIN LICENSE BLOCK --- 2 | # Copyright (c) 2009, Mikio L. Braun 3 | # All rights reserved. 4 | # 5 | # Redistribution and use in source and binary forms, with or without 6 | # modification, are permitted provided that the following conditions are 7 | # met: 8 | # 9 | # * Redistributions of source code must retain the above copyright 10 | # notice, this list of conditions and the following disclaimer. 11 | # 12 | # * Redistributions in binary form must reproduce the above 13 | # copyright notice, this list of conditions and the following 14 | # disclaimer in the documentation and/or other materials provided 15 | # with the distribution. 16 | # 17 | # * Neither the name of the Technische Universitaet Berlin nor the 18 | # names of its contributors may be used to endorse or promote 19 | # products derived from this software without specific prior 20 | # written permission. 21 | # 22 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 | ## --- END LICENSE BLOCK --- 34 | 35 | # In order to pass additional information to the template code, 36 | # subclass Template Context and put the information into instance 37 | # variables. These will then be accessible from the template. 38 | class TemplateContext 39 | 40 | # Generate output from a template file. 41 | # This function also uses the new-line suppressing extension as 42 | # in rails. Put a minus after or before the '%' to suppress the 43 | # previous or following newline. 44 | # 45 | # <%= some expression %>\nblahblah <- newline is output 46 | # <%= some expression -%>\nblahblah <- newline is removed 47 | def generate(template_file) 48 | f = open(template_file) 49 | template = f.read 50 | f.close 51 | 52 | # Add the little newline suppression hack as in rails. 53 | # Minus in front of closing or after opening '%' suppresses newlines. 54 | template.gsub! /-%>\n/, '%>' 55 | template.gsub! /\n<%-/, '<%' 56 | 57 | ERB.new(template).result(binding) 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /scripts/test_fortranwrapper: -------------------------------------------------------------------------------- 1 | #scripts/fortranwrapper com.fhg.la Blas ~/src/blas/ddot.f ~/src/blas/zdotc.f ~/src/blas/saxpy.f ~/src/blas/cdotc.f ~/src/blas/zgemm.f 2 | scripts/fortranwrapper com.fhg.la Blas ~/src/blas/*.f 3 | ant compile createHeaderFiles 4 | make bin/Blas.o -------------------------------------------------------------------------------- /src/main/c/jblas_arch_flavor.c: -------------------------------------------------------------------------------- 1 | /**********************************************************************/ 2 | /* detecting sse level */ 3 | /**********************************************************************/ 4 | 5 | #include 6 | #include 7 | #include "org_jblas_util_ArchFlavor.h" 8 | 9 | /* 10 | * Thanks to the following pages for helping me out on this. 11 | * 12 | * http://softpixel.com/~cwright/programming/simd/cpuid.php 13 | * http://www.gentoo.org/proj/en/hardened/pic-fix-guide.xml 14 | */ 15 | #define cpuid(func,ax,bx,cx,dx) \ 16 | __asm__ __volatile__ (" \ 17 | movl %%ebx, %%edi; \ 18 | cpuid; \ 19 | movl %%ebx, %1; \ 20 | movl %%edi, %%ebx" \ 21 | : "=a" (ax), "=r" (bx), "=c" (cx), "=d" (dx) \ 22 | : "a" (func) \ 23 | : "edi"); 24 | 25 | /* in edx */ 26 | #define SSE (1L << 25) 27 | #define SSE2 (1L << 26) 28 | 29 | /* in ecx */ 30 | #define SSE3 (1L << 0) 31 | 32 | int sse_level() { 33 | int level = -1; 34 | #ifdef HAS_CPUID 35 | int a, b, c, d; 36 | 37 | cpuid(1, a, b, c, d); 38 | 39 | if (d & SSE) { 40 | level = 1; 41 | } 42 | 43 | if (d & SSE2) { 44 | level = 2; 45 | } 46 | 47 | if (c & SSE3) { 48 | level = 3; 49 | } 50 | #endif 51 | return level; 52 | } 53 | 54 | JNIEXPORT jint JNICALL Java_org_jblas_util_ArchFlavor_SSELevel(JNIEnv *env, jclass this) { 55 | return sse_level(); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/c/org_jblas_util_ArchFlavor.h: -------------------------------------------------------------------------------- 1 | /* DO NOT EDIT THIS FILE - it is machine generated */ 2 | #include 3 | /* Header for class org_jblas_util_ArchFlavor */ 4 | 5 | #ifndef _Included_org_jblas_util_ArchFlavor 6 | #define _Included_org_jblas_util_ArchFlavor 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | #undef org_jblas_util_ArchFlavor_SSE 11 | #define org_jblas_util_ArchFlavor_SSE 1L 12 | #undef org_jblas_util_ArchFlavor_SSE2 13 | #define org_jblas_util_ArchFlavor_SSE2 2L 14 | #undef org_jblas_util_ArchFlavor_SSE3 15 | #define org_jblas_util_ArchFlavor_SSE3 3L 16 | #undef org_jblas_util_ArchFlavor_NO_SSE 17 | #define org_jblas_util_ArchFlavor_NO_SSE -1L 18 | /* 19 | * Class: org_jblas_util_ArchFlavor 20 | * Method: SSELevel 21 | * Signature: ()I 22 | */ 23 | JNIEXPORT jint JNICALL Java_org_jblas_util_ArchFlavor_SSELevel 24 | (JNIEnv *, jclass); 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | #endif 30 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ConvertsToDoubleMatrix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.jblas; 7 | 8 | /** 9 | * EXPERIMENTAL, not yet used for anything usefull... . 10 | * 11 | * @author Mikio L. Braun 12 | */ 13 | public interface ConvertsToDoubleMatrix { 14 | public DoubleMatrix convertToDoubleMatrix(); 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ConvertsToFloatMatrix.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.jblas; 7 | 8 | /** 9 | * 10 | * @author mikio 11 | */ 12 | public interface ConvertsToFloatMatrix { 13 | public FloatMatrix convertToFloatMatrix(); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/DoubleFunction.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | /** 40 | * Represents a function on doubles. 41 | */ 42 | public interface DoubleFunction { 43 | /** Compute the function. */ 44 | public double compute(double x); 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/FloatFunction.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | /** 40 | * Represents a function on floats. 41 | */ 42 | public interface FloatFunction { 43 | /** Compute the function. */ 44 | public float compute(float x); 45 | } -------------------------------------------------------------------------------- /src/main/java/org/jblas/Info.java: -------------------------------------------------------------------------------- 1 | package org.jblas; 2 | 3 | /** 4 | * Information class which holds the version String 5 | * 6 | * User: mikio 7 | * Date: 2/12/13 8 | * Time: 3:28 PM 9 | */ 10 | public class Info { 11 | public static String VERSION = "1.2.4"; 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/Trigonometry.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * jBLAS - Light-weight wrapper for ATLAS and LAPACK (http://www.jBLAS.org) 39 | * 40 | * Copyright (C) 2008 jBLAS Project 41 | * 42 | * This library is free software; you can redistribute it and/or 43 | * modify it under the terms of the GNU Lesser General Public 44 | * License as published by the Free Software Foundation; either 45 | * version 2.1 of the License, or (at your option) any later version. 46 | * 47 | * This library is distributed in the hope that it will be useful, 48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 50 | * Lesser General Public License for more details: 51 | * http://www.gnu.org/copyleft/lesser.html#TOC3 52 | * 53 | * Created on May 25, 2008 54 | * $Id$ 55 | */ 56 | package org.jblas; 57 | 58 | /** 59 | * Container for trigonometric functions on vectors and matrices. 60 | * 61 | * @author Johannes Schaback, last edited by $Author$, $Date$ 62 | * @version $Revision$ 63 | */ 64 | public class Trigonometry 65 | { 66 | // yet to be filled 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/Benchmark.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | /** 40 | * 41 | * @author mikio 42 | */ 43 | interface Benchmark { 44 | 45 | public String getName(); 46 | 47 | public BenchmarkResult run(int size, double seconds); 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/BenchmarkResult.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | /** 40 | * 41 | */ 42 | class BenchmarkResult { 43 | long numOps; 44 | double duration; 45 | int iterations; 46 | 47 | BenchmarkResult(long numOps, double duration, int iterations) { 48 | this.numOps = numOps; 49 | this.duration = duration; 50 | this.iterations = iterations; 51 | } 52 | 53 | void printResult() { 54 | System.out.printf("%6.3f GFLOPS (%d iterations in %.1f seconds)%n", 55 | numOps / duration / 1e9, 56 | iterations, 57 | duration); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/JavaDoubleMultiplicationBenchmark.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | import static org.jblas.DoubleMatrix.*; 40 | 41 | /** 42 | * 43 | */ 44 | class JavaDoubleMultiplicationBenchmark implements Benchmark { 45 | 46 | public String getName() { 47 | return "Java matrix multiplication, double precision"; 48 | } 49 | 50 | /** Compute C = A * B */ 51 | private void mmuli(int n, double[] A, double[] B, double[] C) { 52 | for (int i = 0; i < n * n; i++) { 53 | C[i] = 0; 54 | } 55 | 56 | for (int j = 0; j < n; j++) { 57 | int jn = j * n; 58 | for (int k = 0; k < n; k++) { 59 | int kn = k * n; 60 | double bkjn = B[k + jn]; 61 | for (int i = 0; i < n; i++) { 62 | C[i + jn] += A[i + kn] * bkjn; 63 | } 64 | } 65 | } 66 | } 67 | 68 | public BenchmarkResult run(int size, double seconds) { 69 | int counter = 0; 70 | long ops = 0; 71 | 72 | double[] A = randn(size, size).data; 73 | double[] B = randn(size, size).data; 74 | double[] C = randn(size, size).data; 75 | 76 | Timer t = new Timer(); 77 | t.start(); 78 | while (!t.ranFor(seconds)) { 79 | mmuli(size, A, B, C); 80 | counter++; 81 | ops += 2L * size * size * size; 82 | } 83 | t.stop(); 84 | 85 | return new BenchmarkResult(ops, t.elapsedSeconds(), counter); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/JavaFloatMultiplicationBenchmark.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | import static org.jblas.FloatMatrix.randn; 40 | 41 | /** 42 | * 43 | */ 44 | class JavaFloatMultiplicationBenchmark implements Benchmark { 45 | 46 | public String getName() { 47 | return "Java matrix multiplication, single precision"; 48 | } 49 | 50 | /** Compute C = A * B */ 51 | private void mmuli(int n, float[] A, float[] B, float[] C) { 52 | for (int i = 0; i < n * n; i++) { 53 | C[i] = 0; 54 | } 55 | 56 | for (int j = 0; j < n; j++) { 57 | int jn = j * n; 58 | for (int k = 0; k < n; k++) { 59 | int kn = k * n; 60 | float bkjn = B[k + jn]; 61 | for (int i = 0; i < n; i++) { 62 | C[i + jn] += A[i + kn] * bkjn; 63 | } 64 | } 65 | } 66 | } 67 | 68 | public BenchmarkResult run(int size, double seconds) { 69 | int counter = 0; 70 | long ops = 0; 71 | 72 | float[] A = randn(size, size).data; 73 | float[] B = randn(size, size).data; 74 | float[] C = randn(size, size).data; 75 | 76 | Timer t = new Timer(); 77 | t.start(); 78 | while (!t.ranFor(seconds)) { 79 | mmuli(size, A, B, C); 80 | counter++; 81 | ops += 2L * size * size * size; 82 | } 83 | t.stop(); 84 | 85 | return new BenchmarkResult(ops, t.elapsedSeconds(), counter); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/NativeDoubleMultiplicationBenchmark.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | import org.jblas.DoubleMatrix; 40 | import static org.jblas.DoubleMatrix.*; 41 | 42 | /** 43 | * 44 | */ 45 | class NativeDoubleMultiplicationBenchmark implements Benchmark { 46 | 47 | public String getName() { 48 | return "native matrix multiplication, double precision"; 49 | } 50 | 51 | public BenchmarkResult run(int size, double seconds) { 52 | int counter = 0; 53 | long ops = 0; 54 | 55 | DoubleMatrix A = randn(size, size); 56 | DoubleMatrix B = randn(size, size); 57 | DoubleMatrix C = randn(size, size); 58 | 59 | Timer t = new Timer(); 60 | t.start(); 61 | while (!t.ranFor(seconds)) { 62 | A.mmuli(B, C); 63 | counter++; 64 | ops += 2L * size * size * size; 65 | } 66 | t.stop(); 67 | 68 | return new BenchmarkResult(ops, t.elapsedSeconds(), counter); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/NativeFloatMultiplicationBenchmark.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | import org.jblas.FloatMatrix; 40 | import static org.jblas.FloatMatrix.*; 41 | 42 | /** 43 | * 44 | */ 45 | class NativeFloatMultiplicationBenchmark implements Benchmark { 46 | 47 | public String getName() { 48 | return "native matrix multiplication, single precision"; 49 | } 50 | 51 | public BenchmarkResult run(int size, double seconds) { 52 | int counter = 0; 53 | long ops = 0; 54 | 55 | FloatMatrix A = randn(size, size); 56 | FloatMatrix B = randn(size, size); 57 | FloatMatrix C = randn(size, size); 58 | 59 | Timer t = new Timer(); 60 | t.start(); 61 | while (!t.ranFor(seconds)) { 62 | A.mmuli(B, C); 63 | counter++; 64 | ops += 2L * size * size * size; 65 | } 66 | t.stop(); 67 | 68 | return new BenchmarkResult(ops, t.elapsedSeconds(), counter); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/Timer.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.benchmark; 38 | 39 | /** 40 | * 41 | * @author mikio 42 | */ 43 | class Timer { 44 | long startTime; 45 | long stopTime; 46 | 47 | Timer() { 48 | startTime = -1; 49 | stopTime = -1; 50 | } 51 | 52 | void start() { 53 | startTime = System.nanoTime(); 54 | } 55 | 56 | long stop() { 57 | stopTime = System.nanoTime(); 58 | return stopTime - startTime; 59 | } 60 | 61 | boolean ranFor(double seconds) { 62 | return (System.nanoTime() - startTime) / 1e9 >= seconds; 63 | } 64 | 65 | double elapsedSeconds() { 66 | return (stopTime - startTime) / 1e9; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/benchmark/package-info.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /** 38 | * Simple benchmarking tool. 39 | * 40 | *

Run org.jblas.benchmark.Main

. 41 | */ 42 | package org.jblas.benchmark; 43 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/LapackArgumentException.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.exceptions; 38 | 39 | /** 40 | * LapackException for a specific argument. LAPACK routines routinely check 41 | * whether some arguments contain illegal arguments. This exception class 42 | * automatically constructs a message for a given argument index. 43 | */ 44 | public class LapackArgumentException extends LapackException { 45 | /** 46 | * Construct exception for given function and info. Message 47 | * will read "Argument <info> had an illegal value."); 48 | */ 49 | public LapackArgumentException(String function, int info) { 50 | super(function, "Argument " + info + " had an illegal value."); 51 | } 52 | 53 | public LapackArgumentException(String function, String message) { 54 | super(function, message); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/LapackConvergenceException.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas.exceptions; 43 | 44 | /** 45 | * 46 | * @author mikio 47 | */ 48 | public class LapackConvergenceException extends LapackException { 49 | public LapackConvergenceException(String function, String msg) { 50 | super(function, msg); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/LapackException.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.exceptions; 38 | 39 | /** 40 | * Base class for all exceptions within LAPACK. Also reports the function where the 41 | * error is. 42 | */ 43 | public class LapackException extends RuntimeException { 44 | /** Construct new LapackException for the given function. */ 45 | public LapackException(String function) { 46 | super("LAPACK " + function); 47 | } 48 | 49 | /** Construct new Lapack Exception for the given function, with message. */ 50 | public LapackException(String function, String message) { 51 | super("LAPACK " + function + ": " + message); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/LapackPositivityException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package org.jblas.exceptions; 7 | 8 | /** 9 | * 10 | */ 11 | public class LapackPositivityException extends LapackException { 12 | public LapackPositivityException(String fct, String msg) { 13 | super(fct, msg); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/LapackSingularityException.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.exceptions; 38 | 39 | /** 40 | * Exception thrown when matrices are singular. 41 | */ 42 | public class LapackSingularityException extends LapackException { 43 | public LapackSingularityException(String fct, String msg) { 44 | super(fct, msg); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/NoEigenResultException.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009-2013, Mikio L. Braun 4 | * 2013, Alexander Sehlström 5 | * 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are 10 | * met: 11 | * 12 | * * Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 15 | * * Redistributions in binary form must reproduce the above 16 | * copyright notice, this list of conditions and the following 17 | * disclaimer in the documentation and/or other materials provided 18 | * with the distribution. 19 | * 20 | * * Neither the name of the Technische Universitaet Berlin nor the 21 | * names of its contributors may be used to endorse or promote 22 | * products derived from this software without specific prior 23 | * written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | */ 37 | // --- END LICENSE BLOCK --- 38 | 39 | package org.jblas.exceptions; 40 | 41 | public class NoEigenResultException extends RuntimeException { 42 | public NoEigenResultException(String message) { 43 | super(message); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/SizeException.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.exceptions; 38 | 39 | public class SizeException extends RuntimeException { 40 | public SizeException(String message) { 41 | super(message); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/UnsupportedArchitectureException.java: -------------------------------------------------------------------------------- 1 | package org.jblas.exceptions; 2 | 3 | public class UnsupportedArchitectureException extends RuntimeException { 4 | public UnsupportedArchitectureException(String message) { 5 | super(message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/exceptions/package-info.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /** 38 | * jblas related exceptions. 39 | */ 40 | 41 | package org.jblas.exceptions; 42 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/package-info.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /** 38 | * Main linear algebra package. 39 | * 40 | * This package contains the linear algebra packages from jBLAS. 41 | */ 42 | package org.jblas; 43 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/AllRange.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas.ranges; 43 | 44 | /** 45 | * A range over all available indices. Can be used to address whole columns or rows. Like 46 | * the ":" index in matlab. Don't forget to call init() before using this range. 47 | */ 48 | public class AllRange implements Range { 49 | private int lower; 50 | private int upper; 51 | private int value; 52 | private int counter; 53 | 54 | public AllRange() { 55 | } 56 | 57 | public void init(int l, int u) { 58 | lower = l; 59 | upper = u; 60 | value = l; 61 | counter = 0; 62 | } 63 | 64 | public int length() { 65 | return upper - lower; 66 | } 67 | 68 | public int value() { 69 | return value; 70 | } 71 | 72 | public int index() { 73 | return counter; 74 | } 75 | 76 | public void next() { 77 | counter++; 78 | value++; 79 | } 80 | 81 | public boolean hasMore() { 82 | return value < upper; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return String.format("", lower, upper, length(), index(), value()); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/IndicesRange.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas.ranges; 43 | 44 | import org.jblas.*; 45 | 46 | /** 47 | * Range which varies over pre-specified indices. 48 | * 49 | * For example, 50 | *
51 |  *     int[] indices = new int[] { 1, 1, 2, 3, 5, 8, 13 };
52 |  *     Range r = new IndicesRange(indices);
53 | * ranges over the first few Fibonacci numbers. 54 | */ 55 | public class IndicesRange implements Range { 56 | private int[] indices; 57 | private int counter; 58 | 59 | /** Initialize from integer array. */ 60 | public IndicesRange(int[] is) { 61 | indices = is; 62 | } 63 | 64 | public void init(int l, int u) { 65 | counter = 0; 66 | } 67 | 68 | /** 69 | * Initialize from DoubleMatrix. Entries are converted to integers 70 | * by truncation. 71 | */ 72 | public IndicesRange(DoubleMatrix is) { 73 | this(is.toIntArray()); 74 | } 75 | 76 | public int length() { 77 | return indices.length; 78 | } 79 | 80 | public void next() { 81 | counter++; 82 | } 83 | 84 | public int index() { 85 | return counter; 86 | } 87 | 88 | public int value() { 89 | return indices[counter]; 90 | } 91 | 92 | public boolean hasMore() { 93 | return counter < indices.length; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/IntervalRange.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | package org.jblas.ranges; 42 | 43 | /** 44 | * Range which varies from a given interval. Endpoint is exclusive! 45 | * 46 | * "new IntervalRange(0, 3)" enumerates 0, 1, 2. 47 | */ 48 | public class IntervalRange implements Range { 49 | private int start; 50 | private int end; 51 | private int value; 52 | 53 | /** 54 | * Construct new interval range. Endpoints are inclusive. 55 | */ 56 | public IntervalRange(int a, int b) { 57 | start = a; 58 | end = b; 59 | } 60 | 61 | public void init(int lower, int upper) { 62 | value = start; 63 | if (start < lower || end > upper + 1) { 64 | throw new IllegalArgumentException("Bounds " + lower + " to " + upper + " are beyond range interval " + start + " to " + end + "."); 65 | } 66 | } 67 | 68 | public int length() { 69 | return end - start; 70 | } 71 | 72 | public void next() { 73 | value++; 74 | } 75 | 76 | public int index() { 77 | return value - start; 78 | } 79 | 80 | public int value() { 81 | return value; 82 | } 83 | 84 | public boolean hasMore() { 85 | return value < end; 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return String.format("", start, end, length(), index(), value()); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/PointRange.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas.ranges; 43 | 44 | /** 45 | * A PointRange is a range which only has a single point. 46 | */ 47 | public class PointRange implements Range { 48 | private int value; 49 | private boolean consumed; 50 | 51 | /** 52 | * Construct a new PointRange with the one given index. 53 | */ 54 | public PointRange(int v) { 55 | value = v; 56 | } 57 | 58 | public void init(int l, int u) { 59 | consumed = false; 60 | } 61 | 62 | public int length() { 63 | return 1; 64 | } 65 | 66 | public int value() { 67 | return value; 68 | } 69 | 70 | public int index() { 71 | return 0; 72 | } 73 | 74 | public void next() { 75 | consumed = true; 76 | } 77 | 78 | public boolean hasMore() { 79 | return !consumed; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return String.format("", value); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/Range.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas.ranges; 43 | 44 | /** 45 | *

The Range interface represents basically a set of indices. Before using a range 46 | * you have to call init() with the actually available lower and upper bounds, such 47 | * that you can also have an "AllRange" which contains all possible indices.

48 | * 49 | *

Further operations include:

50 | * 51 | *
    52 | *
  • length() - returns total number of elements. 53 | *
  • next() - increase counter (use value()) to retrieve the value. 54 | *
  • index() - get the index of the current value. 55 | *
  • value() - get the current value. 56 | *
  • hasMore() - more indices available. 57 | *
58 | * 59 | *

Typical uses look like this:

60 | *
    for (r.init(lower, upper); r.hasMore(); r.next()) {
61 |  *       System.out.printf("Value number %d is %d%n", index(), value());
62 |  *    }
63 | */ 64 | public interface Range { 65 | /** Initialize Range to available indices */ 66 | public void init(int lower, int upper); 67 | /** Total number of indices. */ 68 | public int length(); 69 | /** Increase counter. */ 70 | public void next(); 71 | /** Consecutive numbering of current index. */ 72 | public int index(); 73 | /** Get current index. */ 74 | public int value(); 75 | /** More indices available? */ 76 | public boolean hasMore(); 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/RangeUtils.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas.ranges; 43 | 44 | import org.jblas.*; 45 | 46 | /** 47 | * A bunch of static functions for making construction of ranges more 48 | * uniform. Basically, we have 49 | *
    50 | *
  • point(3) - a PointRange. 51 | *
  • indices(new int[] {1,2,3,...}) - a Indices Range. 52 | *
  • interval(1, 2) - an interval range. 53 | *
  • all() - an AllRange. 54 | *
  • indices(x) - with a DoubleMatrix. 55 | *
  • find(x) - an index constructed from the non-zero elements of x. 56 | *
57 | * 58 | */ 59 | public class RangeUtils { 60 | /** Construct point range (constant range) with given index. */ 61 | public static Range point(int i) { 62 | return new PointRange(i); 63 | } 64 | 65 | public static Range indices(int[] is) { 66 | return new IndicesRange(is); 67 | } 68 | 69 | public static Range interval(int a, int b) { 70 | return new IntervalRange(a, b); 71 | } 72 | 73 | public static Range all() { 74 | return new AllRange(); 75 | } 76 | 77 | public static Range indices(DoubleMatrix is) { 78 | return new IndicesRange(is); 79 | } 80 | 81 | public static Range find(DoubleMatrix is) { 82 | return new IndicesRange(is.findIndices()); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/ranges/package-info.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /** 38 | * Provide ways to specify indices ranges. 39 | */ 40 | 41 | package org.jblas.ranges; 42 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/util/ArchFlavor.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | package org.jblas.util; 37 | 38 | /** 39 | * 40 | */ 41 | public class ArchFlavor { 42 | 43 | static { 44 | try { 45 | System.loadLibrary("jblas_arch_flavor"); 46 | } catch (UnsatisfiedLinkError e) { 47 | Logger.getLogger().config("ArchFlavor native library not found in path. Copying native library " 48 | + "libjblas_arch_flavor from the archive. Consider installing the library somewhere " 49 | + "in the path (for Windows: PATH, for Linux: LD_LIBRARY_PATH)."); 50 | new org.jblas.util.LibraryLoader().loadLibrary("jblas_arch_flavor", false); 51 | } 52 | } 53 | private static String fixedFlavor = null; 54 | 55 | public static final int SSE = 1; 56 | public static final int SSE2 = 2; 57 | public static final int SSE3 = 3; 58 | public static final int NO_SSE = -1; // for platforms where it isn't applicable. 59 | 60 | public static native int SSELevel(); 61 | 62 | public static String archFlavor() { 63 | if (fixedFlavor != null) 64 | return fixedFlavor; 65 | 66 | String arch = System.getProperty("os.arch"); 67 | String name = System.getProperty("os.name"); 68 | 69 | 70 | if (name.startsWith("Windows") && arch.equals("amd64")) { 71 | return null; 72 | } 73 | else if(arch.equals("i386") || arch.equals("x86") || arch.equals("x86_64") || arch.equals("amd64")) { 74 | switch (SSELevel()) { 75 | case SSE: 76 | return "sse"; 77 | case SSE2: 78 | return "sse2"; 79 | case SSE3: 80 | return "sse3"; 81 | default: 82 | return null; 83 | } 84 | } else { 85 | return null; 86 | } 87 | } 88 | 89 | public static void overrideArchFlavor(String flavor) { 90 | fixedFlavor = flavor; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/util/Functions.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.util; 38 | 39 | public class Functions { 40 | public static double sinc(double x) { 41 | if (x == 0) 42 | return 1.0; 43 | else 44 | return Math.sin(Math.PI * x) / (Math.PI * x); 45 | } 46 | 47 | public static int min(int a, int b) { return a < b ? a : b; } 48 | public static int max(int a, int b) { return a > b ? a : b; } 49 | 50 | private static final double LOG2 = 0.6931471805599453; 51 | 52 | public static double log2(double x) { 53 | return Math.log(x) / LOG2; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/util/Logger.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas.util; 38 | 39 | /** 40 | * 41 | */ 42 | public class Logger { 43 | public static final int ERROR = 5; 44 | public static final int WARNING = 4; 45 | public static final int INFO = 3; 46 | public static final int CONFIG = 2; 47 | public static final int DEBUG = 1; 48 | 49 | public static final String levelNames[] = { 50 | "DEBUG", "CONFIG", "INFO", "WARNING", "ERROR" 51 | }; 52 | 53 | private static Logger theLogger = new Logger(); 54 | private int level; 55 | 56 | private Logger() { 57 | level = INFO; 58 | } 59 | 60 | public static Logger getLogger() { 61 | return theLogger; 62 | } 63 | 64 | public void log(int messageLevel, String msg) { 65 | if (level <= messageLevel) { 66 | System.err.println("-- org.jblas " + levelNames[messageLevel - 1] + " "+ msg); 67 | } 68 | } 69 | 70 | public void debug(String msg) { 71 | log(DEBUG, msg); 72 | } 73 | 74 | public void config(String msg) { 75 | log(CONFIG, msg); 76 | } 77 | 78 | public void info(String msg) { 79 | log(INFO, msg); 80 | } 81 | 82 | public void warning(String msg) { 83 | log(WARNING, msg); 84 | } 85 | 86 | public void error(String msg) { 87 | log(ERROR, msg); 88 | } 89 | 90 | public void setLevel(int level) { 91 | this.level = level; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/util/Random.java: -------------------------------------------------------------------------------- 1 | package org.jblas.util; 2 | 3 | /** 4 | * Created by IntelliJ IDEA. 5 | * User: mikio 6 | * Date: 6/24/11 7 | * Time: 10:45 AM 8 | * To change this template use File | Settings | File Templates. 9 | */ 10 | 11 | public class Random { 12 | private static java.util.Random r = new java.util.Random(); 13 | 14 | public static void seed(long newSeed) { 15 | r = new java.util.Random(newSeed); 16 | } 17 | 18 | public static double nextDouble() { 19 | return r.nextDouble(); 20 | } 21 | 22 | public static float nextFloat() { 23 | return r.nextFloat(); 24 | } 25 | 26 | public static int nextInt(int max) { 27 | return r.nextInt(max); 28 | } 29 | 30 | public static double nextGaussian() { 31 | return r.nextGaussian(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/org/jblas/util/package-info.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /** 38 | * Support classes for jBLAS. 39 | * 40 | * This package contains several support classes. 41 | */ 42 | package org.jblas.util; 43 | -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/aarch64/libjblas.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/aarch64/libjblas.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/aarch64/libjblas_arch_flavor.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/aarch64/libjblas_arch_flavor.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/amd64/libgfortran-4.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/amd64/libgfortran-4.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/amd64/libjblas_arch_flavor.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/amd64/libjblas_arch_flavor.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/amd64/libquadmath-0.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/amd64/libquadmath-0.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/amd64/sse3/libjblas.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/amd64/sse3/libjblas.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/i386/libjblas_arch_flavor.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/i386/libjblas_arch_flavor.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/i386/sse3/libjblas.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/i386/sse3/libjblas.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/ppc64le/libjblas.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/ppc64le/libjblas.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Linux/ppc64le/libjblas_arch_flavor.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Linux/ppc64le/libjblas_arch_flavor.so -------------------------------------------------------------------------------- /src/main/resources/lib/static/Mac OS X/aarch64/libjblas.jnilib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Mac OS X/aarch64/libjblas.jnilib -------------------------------------------------------------------------------- /src/main/resources/lib/static/Mac OS X/aarch64/libjblas_arch_flavor.jnilib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Mac OS X/aarch64/libjblas_arch_flavor.jnilib -------------------------------------------------------------------------------- /src/main/resources/lib/static/Mac OS X/x86_64/libjblas_arch_flavor.jnilib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Mac OS X/x86_64/libjblas_arch_flavor.jnilib -------------------------------------------------------------------------------- /src/main/resources/lib/static/Mac OS X/x86_64/sse3/libjblas.jnilib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Mac OS X/x86_64/sse3/libjblas.jnilib -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/amd64/jblas.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/amd64/jblas.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/amd64/jblas_arch_flavor.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/amd64/jblas_arch_flavor.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/amd64/libgcc_s_sjlj-1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/amd64/libgcc_s_sjlj-1.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/amd64/libgfortran-3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/amd64/libgfortran-3.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/x86/jblas_arch_flavor.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/x86/jblas_arch_flavor.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/x86/libgcc_s_dw2-1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/x86/libgcc_s_dw2-1.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/x86/libgfortran-3.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/x86/libgfortran-3.dll -------------------------------------------------------------------------------- /src/main/resources/lib/static/Windows/x86/sse3/jblas.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jblas-project/jblas/d52d16aecc9608ca00a61bb651a930e7696b5afe/src/main/resources/lib/static/Windows/x86/sse3/jblas.dll -------------------------------------------------------------------------------- /src/test/java/org/jblas/AbstractTestJblas.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | package org.jblas; 37 | 38 | import org.junit.AfterClass; 39 | import org.junit.BeforeClass; 40 | 41 | import java.util.Locale; 42 | 43 | import static java.util.Locale.*; 44 | 45 | /** 46 | * Abstract test class that all tests can extend. 47 | * For now it is used to 48 | */ 49 | public abstract class AbstractTestJblas 50 | { 51 | /** 52 | * This will contain the default locale of the system. 53 | * It will then be used to set the locale back at the end of the tests. 54 | */ 55 | protected static Locale systemDefaultLocale; 56 | 57 | @BeforeClass 58 | public static void setEnglishLocale () 59 | { 60 | systemDefaultLocale = getDefault(); 61 | setDefault( ENGLISH ); 62 | } 63 | 64 | @AfterClass 65 | public static void setBackSystemDefaultLocale () 66 | { 67 | setDefault( systemDefaultLocale ); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/JblasAssert.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2012, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | import static org.junit.Assert.assertArrayEquals; 40 | 41 | public class JblasAssert { 42 | public static void assertEquals(DoubleMatrix expected, DoubleMatrix actual) { 43 | org.junit.Assert.assertEquals(expected.rows, actual.rows); 44 | org.junit.Assert.assertEquals(expected.columns, actual.columns); 45 | assertArrayEquals(expected.data, actual.data, 1e-12); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/SimpleBlasTest.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | import org.junit.Test; 40 | 41 | import static org.junit.Assert.*; 42 | 43 | /** 44 | * Some test for class SimpleBlas 45 | * 46 | * @author Mikio L. Braun 47 | */ 48 | public class SimpleBlasTest { 49 | 50 | @Test 51 | public void testGeev() { 52 | DoubleMatrix A = new DoubleMatrix(2, 2, 3.0, -3.0, 1.0, 1.0); 53 | DoubleMatrix WR = new DoubleMatrix(2); 54 | DoubleMatrix WI = new DoubleMatrix(2); 55 | DoubleMatrix VR = new DoubleMatrix(2,2); 56 | DoubleMatrix VL = new DoubleMatrix(2,2); 57 | 58 | SimpleBlas.geev('V', 'N', A, WR, WI, VR, VL); 59 | 60 | assertTrue(new DoubleMatrix(2, 1, 2.0, 2.0).compare(WR, 1e-6)); 61 | assertTrue(new DoubleMatrix(2, 1, Math.sqrt(2.0), -Math.sqrt(2.0)).compare(WI, 1e-6)); 62 | 63 | /*System.out.printf("WR = %s\n", WR.toString()); 64 | System.out.printf("WI = %s\n", WI.toString()); 65 | System.out.printf("VR = %s\n", VR.toString()); 66 | System.out.printf("VL = %s\n", VL.toString()); 67 | System.out.printf("A = %s\n", A.toString());*/ 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestBlasDoubleComplex.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | package org.jblas; 37 | 38 | import org.junit.*; 39 | import static org.junit.Assert.*; 40 | 41 | public class TestBlasDoubleComplex { 42 | 43 | @Test 44 | public void testZCOPY() { 45 | double[] a = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; 46 | double[] b = new double[6]; 47 | NativeBlas.zcopy(3, a, 0, 1, b, 0, 1); 48 | 49 | for (int i = 0; i < 6; i++) { 50 | assertEquals((double)(i+1), b[i], 1e-6); 51 | } 52 | } 53 | 54 | @Test 55 | public void testZDOTU() { 56 | double[] a = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; 57 | 58 | ComplexDouble c = NativeBlas.zdotu(3, a, 0, 1, a, 0, 1); 59 | assertEquals(new ComplexDouble(-21.0, 88.0), c); 60 | } 61 | 62 | @Test 63 | public void testAxpy() { 64 | double[] x = {0.0, -1.0}; 65 | double[] y = {0.0, 1.0}; 66 | ComplexDouble a = new ComplexDouble(0.0, 1.0); 67 | 68 | // compute x = 2 * x 69 | NativeBlas.zdscal(1, 2.0, x, 0, 1); 70 | assertEquals(new ComplexDouble(0.0, -2.0), new ComplexDouble(x[0], x[1])); 71 | 72 | // compute -I * x + y 73 | NativeBlas.zaxpy(1, a, x, 0, 1, y, 0, 1); 74 | assertEquals(new ComplexDouble(2.0, 1.0), new ComplexDouble(y[0], y[1])); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestComplexDoubleMatrix.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | 42 | package org.jblas; 43 | 44 | import org.junit.*; 45 | import static org.junit.Assert.*; 46 | 47 | /** 48 | * 49 | * @author mikio 50 | */ 51 | 52 | public class TestComplexDoubleMatrix { 53 | 54 | @Test 55 | public void testConstruction() { 56 | ComplexDoubleMatrix A = new ComplexDoubleMatrix(3, 3); 57 | 58 | for (int i = 0; i < A.rows; i++) 59 | for (int j = 0; j < A.columns; j++) 60 | A.put(i, j, new ComplexDouble(i, j)); 61 | 62 | DoubleMatrix R = new DoubleMatrix(3, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 63 | A = new ComplexDoubleMatrix(R, R.transpose()); 64 | 65 | assertEquals(A.real(), R); 66 | assertEquals(A.imag(), R.transpose()); 67 | } 68 | 69 | @Test 70 | public void testComparison() { 71 | ComplexDoubleMatrix A = new ComplexDoubleMatrix(2, 1, 1.0, 2.0, 3.0, 4.0); 72 | ComplexDoubleMatrix B = new ComplexDoubleMatrix(2, 1, 1.0, 2.0, 2.0, 4.0); 73 | 74 | assertEquals(A.eq(B), new ComplexDoubleMatrix(2, 1, 1.0, 0.0, 0.0, 0.0)); 75 | assertEquals(A.ne(B), new ComplexDoubleMatrix(2, 1, 0.0, 0.0, 1.0, 0.0)); 76 | 77 | assertEquals(A.eq(new ComplexDouble(1.0, 2.0)), 78 | new ComplexDoubleMatrix(2, 1, 1.0, 0.0, 0.0, 0.0)); 79 | assertEquals(A.ne(new ComplexDouble(1.0, 2.0)), 80 | new ComplexDoubleMatrix(2, 1, 0.0, 0.0, 1.0, 0.0)); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestComplexFloat.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | import org.junit.Before; 40 | import org.junit.Test; 41 | 42 | import static org.junit.Assert.assertEquals; 43 | import static org.junit.Assert.assertTrue; 44 | 45 | public class TestComplexFloat { 46 | 47 | private ComplexFloat a, b; 48 | 49 | private final double eps = 1e-16; 50 | 51 | @Before 52 | public void setUp() { 53 | a = new ComplexFloat(1, 2); 54 | b = new ComplexFloat(3, 4); 55 | } 56 | 57 | @Test 58 | public void testAdd() { 59 | ComplexFloat c = a.add(b); 60 | 61 | assertEquals(4.0f, c.real(), eps); 62 | assertEquals(6.0f, c.imag(), eps); 63 | } 64 | 65 | @Test 66 | public void testMul() { 67 | ComplexFloat c = a.mul(b); 68 | 69 | assertEquals(-5.0f, c.real(), eps); 70 | assertEquals(10.0f, c.imag(), eps); 71 | } 72 | 73 | @Test 74 | public void testMulAndDiv() { 75 | ComplexFloat d = a.mul(b).div(b); 76 | 77 | assertTrue(new ComplexFloat(1.0f, 2.0f).eq(d)); 78 | 79 | d = a.mul(b).mul(b.inv()); 80 | 81 | assertTrue(new ComplexFloat(1.0f, 2.0f).eq(d)); 82 | } 83 | 84 | @Test 85 | public void testDivByZero() { 86 | a.div(new ComplexFloat(0.0f, 0.0f)); 87 | } 88 | 89 | @Test 90 | public void testSqrt() { 91 | assertEquals(new ComplexFloat(0.0f, 1.0f), new ComplexFloat(-1.0f, 0.0f).sqrt()); 92 | assertEquals(new ComplexFloat(3.0f, 0.0f), new ComplexFloat(9.0f).sqrt()); 93 | assertEquals(new ComplexFloat(0.0f, 3.0f), new ComplexFloat(-9.0f).sqrt()); 94 | assertEquals(new ComplexFloat(1.0f, -4.0f), new ComplexFloat(-15.0f, -8.0f).sqrt()); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestDecompose.java: -------------------------------------------------------------------------------- 1 | package org.jblas; 2 | 3 | import org.junit.*; 4 | import static org.junit.Assert.*; 5 | 6 | /** 7 | * Test class for Decompose 8 | * 9 | * @author Mikio Braun 10 | */ 11 | public class TestDecompose { 12 | @Test 13 | public void luDouble() { 14 | DoubleMatrix A = new DoubleMatrix(3, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 15 | 16 | Decompose.LUDecomposition lu = Decompose.lu(A); 17 | 18 | assertEquals(0.0, (lu.p.mmul(lu.l).mmul(lu.u).sub(A).normmax()), 1e-10); 19 | 20 | assertTrue(lu.l.isLowerTriangular()); 21 | assertTrue(lu.u.isUpperTriangular()); 22 | } 23 | 24 | @Test 25 | public void luFloat() { 26 | FloatMatrix A = new FloatMatrix(3, 3, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f); 27 | 28 | Decompose.LUDecomposition lu = Decompose.lu(A); 29 | 30 | assertEquals(0.0f, (lu.p.mmul(lu.l).mmul(lu.u).sub(A).normmax()), 1e-6f); 31 | 32 | assertTrue(lu.l.isLowerTriangular()); 33 | assertTrue(lu.u.isUpperTriangular()); 34 | } 35 | 36 | @Test 37 | public void qrDouble() { 38 | DoubleMatrix A = new DoubleMatrix(3, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 39 | 40 | Decompose.QRDecomposition qr = Decompose.qr(A); 41 | 42 | assertEquals(0.0, DoubleMatrix.eye(3).sub(qr.q.transpose().mmul(qr.q)).normmax(), 1e-10); 43 | assertTrue(qr.r.isUpperTriangular()); 44 | assertEquals(0.0, A.sub(qr.q.mmul(qr.r)).normmax(), 1e-10); 45 | } 46 | 47 | @Test 48 | public void qrRectangularDouble() { 49 | DoubleMatrix A = new DoubleMatrix(2, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0); 50 | 51 | Decompose.QRDecomposition qr = Decompose.qr(A); 52 | 53 | assertEquals(0.0, DoubleMatrix.eye(2).sub(qr.q.transpose().mmul(qr.q)).normmax(), 1e-10); 54 | assertTrue(qr.r.isUpperTriangular()); 55 | assertEquals(0.0, A.sub(qr.q.mmul(qr.r)).normmax(), 1e-10); 56 | } 57 | 58 | @Test 59 | public void qrRectangular2Double() { 60 | DoubleMatrix A = new DoubleMatrix(4, 2, 1.0, 2.0, 2.5, 3.0, 4.0, 5.0, 6.0, 6.5); 61 | 62 | Decompose.QRDecomposition qr = Decompose.qr(A); 63 | 64 | DoubleMatrix qtq = qr.q.transpose().mmul(qr.q); 65 | 66 | assertEquals(0.0, DoubleMatrix.eye(4).sub(qtq).normmax(), 1e-10); 67 | assertTrue(qr.r.isUpperTriangular()); 68 | assertEquals(0.0, A.sub(qr.q.mmul(qr.r)).normmax(), 1e-10); 69 | } 70 | 71 | 72 | @Test 73 | public void qrFloat() { 74 | FloatMatrix A = new FloatMatrix(3, 3, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f); 75 | 76 | Decompose.QRDecomposition qr = Decompose.qr(A); 77 | 78 | assertEquals(0.0f, FloatMatrix.eye(3).sub(qr.q.transpose().mmul(qr.q)).normmax(), 1e-5f); 79 | assertTrue(qr.r.isUpperTriangular()); 80 | assertEquals(0.0f, A.sub(qr.q.mmul(qr.r)).normmax(), 1e-5f); 81 | } 82 | 83 | @Test 84 | public void qrRectangularFloat() { 85 | //FloatMatrix A = new FloatMatrix(2, 3, 1.0f, 2.0f, 3.0f, 4.0f, 7.0f, 6.0f); 86 | FloatMatrix A = FloatMatrix.rand(2, 3); 87 | 88 | Decompose.QRDecomposition qr = Decompose.qr(A); 89 | 90 | assertEquals(0.0f, FloatMatrix.eye(2).sub(qr.q.transpose().mmul(qr.q)).normmax(), 1e-5f); 91 | assertTrue(qr.r.isUpperTriangular()); 92 | assertEquals(0.0f, A.sub(qr.q.mmul(qr.r)).normmax(), 1e-5f); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestEigen.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | /* 38 | * To change this template, choose Tools | Templates 39 | * and open the template in the editor. 40 | */ 41 | package org.jblas; 42 | 43 | import org.junit.Test; 44 | 45 | import static org.junit.Assert.*; 46 | 47 | /** 48 | * Test Class for org.jblas.Eigen 49 | * 50 | * @author mikio 51 | */ 52 | 53 | public class TestEigen { 54 | 55 | private final double eps = 1e-10; 56 | 57 | @Test 58 | public void testEigenvalues() { 59 | DoubleMatrix A = new DoubleMatrix(2, 2, 3.0, -3.0, 1.0, 1.0); 60 | 61 | ComplexDoubleMatrix E = Eigen.eigenvalues(A); 62 | 63 | ComplexDoubleMatrix[] EV = Eigen.eigenvectors(A); 64 | 65 | ComplexDoubleMatrix X = EV[0]; 66 | ComplexDoubleMatrix L = EV[1]; 67 | 68 | assertEquals(0.0, A.toComplex().mmul(X).sub(X.mmul(L)).norm2(), eps); 69 | } 70 | 71 | @Test 72 | public void testSymmetricEigenvalues() { 73 | DoubleMatrix A = new DoubleMatrix(new double[][]{ 74 | {3.0, 1.0, 0.5}, 75 | {1.0, 3.0, 1.0}, 76 | {0.5, 1.0, 3.0} 77 | }); 78 | 79 | DoubleMatrix B = new DoubleMatrix(new double[][]{ 80 | {2.0, 0.1, 0.0}, 81 | {0.1, 2.0, 0.1}, 82 | {0.0, 0.1, 2.0} 83 | }); 84 | 85 | DoubleMatrix[] results = Eigen.symmetricGeneralizedEigenvectors(A, B); 86 | 87 | DoubleMatrix V = results[0]; 88 | DoubleMatrix L = results[1]; 89 | 90 | DoubleMatrix LHS = A.mmul(V); 91 | DoubleMatrix RHS = B.mmul(V).mmul(DoubleMatrix.diag(L)); 92 | 93 | assertEquals(0.0, LHS.sub(RHS).normmax(), eps); 94 | 95 | DoubleMatrix eigenvalues = Eigen.symmetricGeneralizedEigenvalues(A, B); 96 | 97 | assertEquals(0.0, eigenvalues.sub(L).normmax(), eps); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestGeometry.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | import org.junit.Test; 40 | 41 | import static org.junit.Assert.*; 42 | 43 | public class TestGeometry { 44 | 45 | private final double eps = 1e-10; 46 | 47 | @Test 48 | public void testCenter() { 49 | DoubleMatrix x = new DoubleMatrix(3, 1, 1.0, 2.0, 3.0); 50 | 51 | Geometry.center(x); 52 | 53 | assertEquals(new DoubleMatrix(3, 1, -1.0, 0.0, 1.0), x); 54 | 55 | DoubleMatrix M = new DoubleMatrix(new double[][]{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}}); 56 | 57 | DoubleMatrix MR = Geometry.centerRows(M.dup()); 58 | DoubleMatrix MC = Geometry.centerColumns(M.dup()); 59 | 60 | assertEquals(new DoubleMatrix(new double[][]{{-1.0, 0.0, 1.0}, {-1.0, 0.0, 1.0}, {-1.0, 0.0, 1.0}}), MR); 61 | assertEquals(new DoubleMatrix(new double[][]{{-3.0, -3.0, -3.0}, {0.0, 0.0, 0.0}, {3.0, 3.0, 3.0}}), MC); 62 | } 63 | 64 | @Test 65 | public void testPwDist() { 66 | DoubleMatrix M = new DoubleMatrix(3, 5, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0); 67 | 68 | DoubleMatrix D = Geometry.pairwiseSquaredDistances(M, M); 69 | 70 | assertEquals(0.0, new DoubleMatrix(5, 5, 71 | 0.0, 27.0, 108.0, 243.0, 432.0, 72 | 27.0, 0.0, 27.0, 108.0, 243.0, 73 | 108.0, 27.0, 0.0, 27.0, 108.0, 74 | 243.0, 108.0, 27.0, 0.0, 27.0, 75 | 432.0, 243.0, 108.0, 27.0, 0.0).distance2(D), eps); 76 | 77 | M = M.transpose(); 78 | 79 | D = Geometry.pairwiseSquaredDistances(M, M); 80 | 81 | assertEquals(0.0, new DoubleMatrix(3, 3, 82 | 0.0, 5.0, 20.0, 5.0, 0.0, 5.0, 20.0, 5.0, 0.0).distance2(D), eps); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestSingular.java: -------------------------------------------------------------------------------- 1 | package org.jblas; 2 | 3 | import org.junit.*; 4 | import static org.junit.Assert.*; 5 | 6 | /** 7 | * Test cases for class Singular 8 | * 9 | * Singular value decompositions 10 | * 11 | * @author Mikio L. Braun 12 | */ 13 | public class TestSingular { 14 | @Test 15 | public void testComplexDoubleSVD() { 16 | ComplexDoubleMatrix A = new ComplexDoubleMatrix(3, 4); 17 | 18 | for (int i = 0; i < A.rows; i++) 19 | for (int j = 0; j < A.columns; j++) 20 | A.put(i, j, (double) i, (double) j); 21 | 22 | ComplexDoubleMatrix[] USV = Singular.sparseSVD(A); 23 | ComplexDoubleMatrix U = USV[0]; 24 | ComplexDoubleMatrix S = ComplexDoubleMatrix.diag(USV[1]); 25 | ComplexDoubleMatrix V = USV[2]; 26 | 27 | assertEquals(3, U.rows); 28 | assertEquals(3, U.columns); 29 | assertEquals(4, V.rows); 30 | assertEquals(3, V.columns); 31 | assertEquals(0.0, U.mmul(S).mmul(V.hermitian()).sub(A).normmax(), 1e-10); 32 | 33 | USV = Singular.fullSVD(A); 34 | U = USV[0]; 35 | S = ComplexDoubleMatrix.diag(USV[1], 3, 4); 36 | V = USV[2]; 37 | 38 | assertEquals(3, U.rows); 39 | assertEquals(3, U.columns); 40 | assertEquals(3, S.rows); 41 | assertEquals(4, S.columns); 42 | assertEquals(4, V.rows); 43 | assertEquals(4, V.columns); 44 | 45 | assertEquals(0.0, U.mmul(S).mmul(V.hermitian()).sub(A).normmax(), 1e-10); 46 | } 47 | 48 | @Test 49 | public void testComplexFloatSVD() { 50 | ComplexFloatMatrix A = new ComplexFloatMatrix(3, 4); 51 | 52 | for (int i = 0; i < A.rows; i++) 53 | for (int j = 0; j < A.columns; j++) 54 | A.put(i, j, (float) i, (float) j); 55 | 56 | ComplexFloatMatrix[] USV = Singular.sparseSVD(A); 57 | ComplexFloatMatrix U = USV[0]; 58 | ComplexFloatMatrix S = ComplexFloatMatrix.diag(USV[1]); 59 | ComplexFloatMatrix V = USV[2]; 60 | 61 | assertEquals(3, U.rows); 62 | assertEquals(3, U.columns); 63 | assertEquals(4, V.rows); 64 | assertEquals(3, V.columns); 65 | assertEquals(0.0, U.mmul(S).mmul(V.hermitian()).sub(A).normmax(), 1e-4); 66 | 67 | USV = Singular.fullSVD(A); 68 | U = USV[0]; 69 | S = ComplexFloatMatrix.diag(USV[1], 3, 4); 70 | V = USV[2]; 71 | 72 | assertEquals(3, U.rows); 73 | assertEquals(3, U.columns); 74 | assertEquals(3, S.rows); 75 | assertEquals(4, S.columns); 76 | assertEquals(4, V.rows); 77 | assertEquals(4, V.columns); 78 | 79 | assertEquals(0.0, U.mmul(S).mmul(V.hermitian()).sub(A).normmax(), 1e-4); 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/TestSolve.java: -------------------------------------------------------------------------------- 1 | // --- BEGIN LICENSE BLOCK --- 2 | /* 3 | * Copyright (c) 2009, Mikio L. Braun 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * * Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * * Redistributions in binary form must reproduce the above 14 | * copyright notice, this list of conditions and the following 15 | * disclaimer in the documentation and/or other materials provided 16 | * with the distribution. 17 | * 18 | * * Neither the name of the Technische Universitaet Berlin nor the 19 | * names of its contributors may be used to endorse or promote 20 | * products derived from this software without specific prior 21 | * written permission. 22 | * 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | */ 35 | // --- END LICENSE BLOCK --- 36 | 37 | package org.jblas; 38 | 39 | import org.junit.*; 40 | import static org.junit.Assert.*; 41 | 42 | /** 43 | * Tests for methods in Solve 44 | * 45 | * Created: 1/18/13, 12:10 PM 46 | * 47 | * @author Mikio L. Braun 48 | */ 49 | public class TestSolve { 50 | @Test 51 | public void testLeastSquaresDouble() { 52 | DoubleMatrix A = new DoubleMatrix(3, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0); 53 | DoubleMatrix B = new DoubleMatrix(3, 1, 3.0, 2.0, -1.0); 54 | 55 | assertEquals(0.0, new DoubleMatrix(3, 1, -23.0 / 9, -2.0 / 3, 11.0 / 9).sub(Solve.solveLeastSquares(A, B)).normmax(), 1e-10); 56 | } 57 | 58 | @Test 59 | public void testLeastSquaresFloat() { 60 | FloatMatrix A = new FloatMatrix(3, 3, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f); 61 | FloatMatrix B = new FloatMatrix(3, 1, 3.0f, 2.0f, -1.0f); 62 | 63 | assertEquals(0.0f, new FloatMatrix(3, 1, -23.0f / 9, -2.0f / 3, 11.0f / 9).sub(Solve.solveLeastSquares(A, B)).normmax(), 1e-5f); 64 | } 65 | 66 | @Test 67 | public void testLeastSquaresWideMatrixDouble() { 68 | DoubleMatrix A = new DoubleMatrix(2, 3, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0); 69 | DoubleMatrix B = new DoubleMatrix(2, 1, 1.0, -1.0); 70 | 71 | assertEquals(0.0, B.sub(A.mmul(Solve.solveLeastSquares(A, B))).normmax(), 1e-10); 72 | } 73 | 74 | @Test 75 | public void testPinvDouble() { 76 | DoubleMatrix A = new DoubleMatrix(3, 2, 1.0, 3.0, 5.0, 2.0, 4.0, 6.0); 77 | 78 | DoubleMatrix pinvA = Solve.pinv(A); 79 | assertEquals(0.0, A.mmul(pinvA).mmul(A).sub(A).normmax(), 1e-10); 80 | assertEquals(0.0, pinvA.mmul(A).mmul(pinvA).sub(pinvA).normmax(), 1e-10); 81 | 82 | DoubleMatrix At = A.transpose(); 83 | DoubleMatrix pinvAt = Solve.pinv(At); 84 | 85 | assertEquals(0.0, At.mmul(pinvAt).mmul(At).sub(At).normmax(), 1e-10); 86 | assertEquals(0.0, pinvAt.mmul(At).mmul(pinvAt).sub(pinvAt).normmax(), 1e-10); 87 | } 88 | 89 | @Test 90 | public void testPinvFloat() { 91 | FloatMatrix A = new FloatMatrix(3, 2, 1.0f, 3.0f, 5.0f, 2.0f, 4.0f, 6.0f); 92 | 93 | FloatMatrix pinvA = Solve.pinv(A); 94 | assertEquals(0.0f, A.mmul(pinvA).mmul(A).sub(A).normmax(), 1e-5f); 95 | assertEquals(0.0f, pinvA.mmul(A).mmul(pinvA).sub(pinvA).normmax(), 1e-5f); 96 | } 97 | 98 | /* 99 | @Test 100 | public void randomSized() { 101 | for (int i = 0; i < 1000; i++) { 102 | Random r = new Random(); 103 | int m = 362; // r.nextInt(1000); 104 | int n = 8; // r.nextInt(1000); 105 | DoubleMatrix A = DoubleMatrix.rand(m, n); 106 | 107 | System.out.printf("Pinv for %d * %d matrix...\n", A.rows, A.columns); 108 | double t = System.nanoTime(); 109 | Solve.pinv(A); 110 | System.out.printf("Pinv for %d * %d matrix took %.1fs\n", A.rows, A.columns, (System.nanoTime() - t) / 1e9); 111 | } 112 | } 113 | */ 114 | } 115 | -------------------------------------------------------------------------------- /src/test/java/org/jblas/ranges/RangeTest.java: -------------------------------------------------------------------------------- 1 | package org.jblas.ranges; 2 | 3 | import org.jblas.DoubleMatrix; 4 | import org.junit.Test; 5 | import static org.jblas.ranges.RangeUtils.*; 6 | import static org.jblas.JblasAssert.*; 7 | 8 | /** 9 | * Testing the ranges facility. 10 | * 11 | * @author Mikio L. Braun 12 | * File created June 12, 2012, 16:04 13 | */ 14 | 15 | public class RangeTest { 16 | 17 | private DoubleMatrix A = new DoubleMatrix(3, 4, 1.0, 2.0, 3.0, 18 | 4.0, 5.0, 6.0, 19 | 7.0, 8.0, 9.0, 20 | 10.0, 11.0, 12.0); 21 | 22 | @Test 23 | public void allRange() { 24 | assertEquals(A, A.get(all(), all())); 25 | assertEquals(new DoubleMatrix(3, 1, 1.0, 2.0, 3.0), A.get(all(), 0)); 26 | assertEquals(new DoubleMatrix(3, 1, 7.0, 8.0, 9.0), A.get(all(), 2)); 27 | } 28 | 29 | @Test 30 | public void pointRange() { 31 | assertEquals(DoubleMatrix.scalar(8.0), A.get(point(1), point(2))); 32 | } 33 | 34 | @Test 35 | public void indicesRange() { 36 | assertEquals(new DoubleMatrix(1, 5, 1.0, 7.0, 4.0, 10.0, 1.0), A.get(0, indices(new int[] {0, 2, 1, 3, 0}))); 37 | } 38 | 39 | @Test 40 | public void mixedRanges() { 41 | assertEquals(new DoubleMatrix(3, 2, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0), A.get(all(), interval(1, 3))); 42 | assertEquals(new DoubleMatrix(2, 1, 11.0, 12.0), A.get(interval(1, 3), point(3))); 43 | } 44 | } 45 | --------------------------------------------------------------------------------