'
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 |
--------------------------------------------------------------------------------