Skip to content

Commit f5c87a1

Browse files
authored
Merge pull request #558 from prasunanand/jruby_test_rebase
JRuby port of NMatrix
2 parents 4bc0a2a + 36a1cf5 commit f5c87a1

40 files changed

+3727
-256
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ doc/
2727
docs/
2828
pkg/
2929
.autotest
30+
ext/nmatrix_java/vendor/
31+
ext/nmatrix_java/target/
32+
ext/nmatrix_java/build/
33+
ext/nmatrix_java/target/
34+
*.class
35+
*.jar

.travis.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ rvm:
2020
- 2.3.0-clang
2121
- ruby-head-clang
2222
# JRuby versions --- experimental, pending merging Prasun's GSoC project.
23-
# - jruby-9.0.0.0 # earliest supported version (uncomment when jruby-head is passing)
23+
- jruby-9.0.5.0 # earliest supported version (uncomment when jruby-head is passing)
2424
- jruby-head # latest supported JRuby
2525
# Make sure to add exclude lines for new JRuby versions below.
2626

@@ -42,11 +42,12 @@ matrix:
4242
env: USE_OPENBLAS=1
4343
- rvm: jruby-head
4444
env: USE_REF=1
45-
#- rvm: jruby-9.0.0.0
46-
# env:
47-
# - USE_ATLAS=1
48-
# - USE_OPENBLAS=1
49-
# - USE_REF=1
45+
- rvm: jruby-9.0.5.0
46+
env: USE_ATLAS=1
47+
- rvm: jruby-9.0.5.0
48+
env: USE_OPENBLAS=1
49+
- rvm: jruby-9.0.5.0
50+
env: USE_REF=1
5051
# NOTE: The following two ruby versions on OSX are currently unavailable
5152
- os: osx
5253
rvm: 2.0.0-p648
@@ -106,8 +107,6 @@ matrix:
106107
- ruby_version=2.1.8 USE_OPENBLAS=1
107108
allow_failures:
108109
# trunk
109-
- rvm: jruby-head
110-
#- rvm: jruby-9.0.0.0
111110
- rvm: ruby-head
112111
- rvm: ruby-head-clang
113112
- os: osx

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ $ rake spec
6262
This will install all dependencies, compile the extension and run the
6363
specs.
6464

65+
For **JRuby**
66+
67+
```bash
68+
$ mkdir ext/nmatrix_java/vendor
69+
Download commons_math.3.6.1 jar and place it in ext/nmatrix_java/vendor directory
70+
$ mkdir -p ext/nmatrix_java/build/class
71+
$ mkdir ext/nmatrix_java/target
72+
$ rake jruby
73+
```
74+
6575
If everything's fine until now, you can create a new branch to work on
6676
your feature:
6777

README.rdoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ To install:
6767

6868
bundle exec rake install
6969

70+
=== JRuby
71+
72+
First, you need to download Apache Commons Math 3.6.1 (the JAR, which
73+
you can find in the binary package). For example, in the NMatrix
74+
directory, do:
75+
76+
wget https://www.apache.org/dist/commons/math/binaries/commons-math3-3.6.1-bin.tar.gz
77+
tar zxvf commons-math3-3.6.1-bin.tar.gz
78+
mkdir ext/nmatrix_java/vendor/
79+
cp commons-math3-3.6.1/commons-math3-3.6.1.jar ext/nmatrix_java/vendor/
80+
81+
Next, create build directories:
82+
83+
mkdir -p ext/nmatrix_java/build/class
84+
mkdir ext/nmatrix_java/target
85+
86+
Finally, compile and package as jar.
87+
88+
rake jruby
89+
90+
=== Plugins
91+
7092
The commands above build and install only the core +nmatrix+ gem. If
7193
you want to build one or more of the plugin gems (+nmatrix-atlas+,
7294
+nmatrix-lapacke+) in addition to the core nmatrix gem, use the

Rakefile

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ namespace :spec do
7373
gemspecs.each do |gemspec|
7474
test_files = gemspec.test_files
7575
test_files.keep_if { |file| file =~ /_spec\.rb$/ }
76+
test_files -= ['spec/nmatrix_yale_spec.rb', 'spec/blas_spec.rb', 'spec/lapack_core_spec.rb'] if /java/ === RUBY_PLATFORM
7677
next if test_files.empty?
7778
spec_tasks << gemspec.name
7879
RSpec::Core::RakeTask.new(gemspec.name) do |spec|
@@ -273,27 +274,83 @@ RDoc::Task.new do |rdoc|
273274
rdoc.options << "--exclude=lib/nmatrix/rspec.rb"
274275
end
275276

276-
namespace :travis do
277-
task :env do
278-
puts "\n# Build environment:"
279-
%w[
280-
CC CXX
281-
USE_ATLAS USE_OPENBLAS USE_REF NO_EXTERNAL_LIB
282-
TRAVIS_OS_NAME TRAVIS_BRANCH TRAVIS_COMMIT TRAVIS_PULL_REQUEST
283-
].each do |name|
284-
puts "- #{name}: #{ENV[name]}"
277+
# jruby tasks
278+
279+
namespace :jruby do
280+
281+
PROJECT_DIR = File.expand_path(".",Dir.pwd)
282+
283+
BUILD_DIR = "build"
284+
CLASSES_DIR = "../build/classes"
285+
TEST_CLASSES_DIR = "build/testClasses"
286+
287+
JRUBY_DIR = "#{PROJECT_DIR}/ext/nmatrix_java"
288+
VENDOR_DIR = "#{JRUBY_DIR}/vendor"
289+
TARGET_DIR = "#{JRUBY_DIR}/target"
290+
291+
jars = Dir["#{VENDOR_DIR}/*.jar"]
292+
293+
desc 'Compile java classes'
294+
task :javac do
295+
unless RUBY_PLATFORM == 'java'
296+
abort 'Please run with JRuby'
285297
end
298+
sh "mkdir -p #{JRUBY_DIR}/build/classes"
299+
Dir.chdir("#{JRUBY_DIR}/nmatrix")
300+
classes = Dir['**/*.java']
301+
sh "javac -classpath #{jars.join(':')} -d #{CLASSES_DIR} #{classes.join(' ')}"
302+
end
286303

287-
require 'rbconfig'
288-
puts "\n# RbConfig::MAKEFILE_CONFIG values:"
289-
%w[
290-
CC CXX CPPFLAGS CFLAGS CXXFLAGS
291-
].each do |name|
292-
puts "- #{name}: #{RbConfig::MAKEFILE_CONFIG[name]}"
304+
desc 'Package java classes in a jar file'
305+
task :jar do
306+
unless RUBY_PLATFORM == 'java'
307+
abort 'Please run with JRuby'
293308
end
309+
sh "mkdir -p #{TARGET_DIR}"
310+
Dir.chdir("#{JRUBY_DIR}/build/classes")
311+
classes = Dir['**/*.class']
312+
sh "jar -cf #{TARGET_DIR}/nmatrix.jar #{classes.join(' ')}"
313+
end
294314

295-
cc = RbConfig::MAKEFILE_CONFIG['CC']
296-
puts "\n$ #{cc} -v\n#{`#{cc} -v 2>&1`}"
315+
task :all => [:javac, :jar]
316+
end
317+
318+
desc "Compile java classes and Package them in a jar file"
319+
task :jruby => 'jruby:all'
320+
321+
namespace :travis do
322+
task :env do
323+
if /java/ === RUBY_PLATFORM
324+
puts "Building for jruby"
325+
sh "mkdir ext/nmatrix_java/vendor"
326+
puts "Downloading tar file."
327+
sh "wget http://www-eu.apache.org/dist//commons/math/binaries/commons-math3-3.6.1-bin.tar.gz"
328+
puts "Unzipping tar file."
329+
sh "tar -zxf commons-math3-3.6.1-bin.tar.gz"
330+
puts "Deleting tar file."
331+
sh "rm commons-math3-3.6.1-bin.tar.gz"
332+
sh "cp -r commons-math3-3.6.1/commons-math3-3.6.1.jar ext/nmatrix_java/vendor"
333+
else
334+
puts "\n# Build environment:"
335+
%w[
336+
CC CXX
337+
USE_ATLAS USE_OPENBLAS USE_REF NO_EXTERNAL_LIB
338+
TRAVIS_OS_NAME TRAVIS_BRANCH TRAVIS_COMMIT TRAVIS_PULL_REQUEST
339+
].each do |name|
340+
puts "- #{name}: #{ENV[name]}"
341+
end
342+
343+
require 'rbconfig'
344+
puts "\n# RbConfig::MAKEFILE_CONFIG values:"
345+
%w[
346+
CC CXX CPPFLAGS CFLAGS CXXFLAGS
347+
].each do |name|
348+
puts "- #{name}: #{RbConfig::MAKEFILE_CONFIG[name]}"
349+
end
350+
351+
cc = RbConfig::MAKEFILE_CONFIG['CC']
352+
puts "\n$ #{cc} -v\n#{`#{cc} -v 2>&1`}"
353+
end
297354
end
298355
end
299356

ext/nmatrix_java/README.md

Whitespace-only changes.

ext/nmatrix_java/Rakefile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
PROJECT_DIR = File.expand_path(".",Dir.pwd)
2+
# puts PROJECT_DIR
3+
4+
BUILD_DIR = "build"
5+
CLASSES_DIR = "../build/classes"
6+
TEST_CLASSES_DIR = "build/testClasses"
7+
8+
VENDOR_DIR = "#{PROJECT_DIR}/vendor"
9+
TARGET_DIR = "#{PROJECT_DIR}/target"
10+
# puts VENDOR_DIR
11+
12+
jars = Dir["#{VENDOR_DIR}/*.jar"]
13+
# puts jars
14+
15+
unless RUBY_PLATFORM == 'java'
16+
abort 'Please run with JRuby'
17+
end
18+
19+
#-----------------------------------------------------------------------------------------
20+
21+
desc 'Compile java classes'
22+
task :javac do
23+
24+
Dir.chdir("nmatrix")
25+
classes = Dir['**/*.java']
26+
sh "javac -classpath #{jars.join(':')} -d #{CLASSES_DIR} #{classes.join(' ')}"
27+
# sh "javac -d #{CLASSES_DIR} #{classes.join(' ')}"
28+
29+
end
30+
31+
#-----------------------------------------------------------------------------------------
32+
desc 'Make jar file'
33+
task :jar do
34+
35+
Dir.chdir("build/classes")
36+
classes = Dir['**/*.class']
37+
# p classes
38+
sh "jar -cf #{TARGET_DIR}/nmatrix.jar #{classes.join(' ')}"
39+
40+
end
41+
42+
#-----------------------------------------------------------------------------------------
43+
44+
45+
desc 'Compile test classes'
46+
task :compileTest do
47+
48+
classes = Dir['**/*.java']
49+
sh "javac -classpath #{jars.join(':')} -d #{TEST_CLASSES_DIR} #{classes.join(' ')}"
50+
# sh "javac -d #{CLASSES_DIR} #{classes.join(' ')}"
51+
52+
end
53+
54+
#-----------------------------------------------------------------------------------------
55+
56+
desc 'Run junit tests'
57+
task :test do
58+
59+
Dir.chdir("build/classes")
60+
classes = Dir['**/*.class']
61+
# p classes
62+
sh "javac -classpath #{TARGET_DIR}/nmatrix.jar -d #{classes.join(' ')}"
63+
64+
end
65+
66+
#-----------------------------------------------------------------------------------------
67+
68+
task :default => :javac

ext/nmatrix_java/nmatrix/data/Complex.java

Whitespace-only changes.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import org.apache.commons.math3.util.FastMath;
2+
import org.apache.commons.math3.special.Erf;
3+
import org.apache.commons.math3.special.Gamma;
4+
5+
public class MathHelper{
6+
7+
public static double[] log(double base, double[] arr){
8+
double[] result = new double[arr.length];
9+
for(int i = 0; i< arr.length; i++){
10+
result[i] = FastMath.log(base, arr[i]);
11+
}
12+
return result;
13+
}
14+
15+
public static double[] erf(double[] arr){
16+
double[] result = new double[arr.length];
17+
for(int i = 0; i< arr.length; i++){
18+
result[i] = Erf.erf(arr[i]);
19+
}
20+
return result;
21+
}
22+
23+
public static double[] erfc(double[] arr){
24+
double[] result = new double[arr.length];
25+
for(int i = 0; i< arr.length; i++){
26+
result[i] = Erf.erfc(arr[i]);
27+
}
28+
return result;
29+
}
30+
31+
public static double[] gamma(double[] arr){
32+
double[] result = new double[arr.length];
33+
for(int i = 0; i< arr.length; i++){
34+
result[i] = Gamma.gamma(arr[i]);
35+
}
36+
return result;
37+
}
38+
39+
public static double[] round(double[] arr){
40+
double[] result = new double[arr.length];
41+
for(int i = 0; i< arr.length; i++){
42+
result[i] = Math.round(arr[i]);
43+
}
44+
return result;
45+
}
46+
47+
public static double[] ldexp(double[] arr1, double[] arr){
48+
double[] result = new double[arr1.length];
49+
for(int i = 0; i< arr1.length; i++){
50+
result[i] = arr1[i] * Math.pow(2, arr[i]);
51+
}
52+
return result;
53+
}
54+
55+
public static double[] ldexpScalar(double val, double[] arr){
56+
double[] result = new double[arr.length];
57+
for(int i = 0; i< arr.length; i++){
58+
result[i] = val * Math.pow(2, arr[i]);
59+
}
60+
return result;
61+
}
62+
63+
public static double[] ldexpScalar2(double val, double[] arr){
64+
double[] result = new double[arr.length];
65+
for(int i = 0; i< arr.length; i++){
66+
result[i] = arr[i] * Math.pow(2, val);
67+
}
68+
return result;
69+
}
70+
71+
public static double[] hypot(double[] arr1, double[] arr2){
72+
double[] result = new double[arr1.length];
73+
for(int i = 0; i< arr1.length; i++){
74+
result[i] = Math.sqrt(arr2[i] * arr2[i] + arr1[i] * arr1[i]);
75+
}
76+
return result;
77+
}
78+
79+
public static double[] hypotScalar(double val, double[] arr){
80+
double[] result = new double[arr.length];
81+
for(int i = 0; i< arr.length; i++){
82+
result[i] = Math.sqrt(arr[i] * arr[i] + val * val);
83+
}
84+
return result;
85+
}
86+
87+
public static double[] atan2(double[] arr1, double[] arr2){
88+
double[] result = new double[arr1.length];
89+
for(int i = 0; i< arr1.length; i++){
90+
result[i] = Math.atan2(arr2[i], arr1[i]);
91+
}
92+
return result;
93+
}
94+
95+
public static double[] atan2Scalar(double val, double[] arr){
96+
double[] result = new double[arr.length];
97+
for(int i = 0; i< arr.length; i++){
98+
result[i] = Math.atan2(val, arr[i]);
99+
}
100+
return result;
101+
}
102+
103+
public static double[] atan2Scalar2(double val, double[] arr){
104+
double[] result = new double[arr.length];
105+
for(int i = 0; i< arr.length; i++){
106+
result[i] = Math.atan2(arr[i], val);
107+
}
108+
return result;
109+
}
110+
111+
}

0 commit comments

Comments
 (0)