Skip to content

Commit

Permalink
* Update sample code for GSL with a more complex example (issue #636)
Browse files Browse the repository at this point in the history
  • Loading branch information
saudet committed Oct 21, 2018
1 parent 4200f91 commit 0cc6ee6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Update sample code for GSL with a more complex example ([issue #636](https://github.com/bytedeco/javacpp-presets/issues/636))
* Fix CUDA build for OpenCV on Mac OS X missing `libopencv_cudev.dylib` ([issue #626](https://github.com/bytedeco/javacpp-presets/issues/626))
* Upgrade presets for HDF5 1.10.4, Tesseract 4.0.0-rc3, TensorFlow 1.12.0-rc0, and their dependencies

Expand Down
43 changes: 33 additions & 10 deletions gsl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Java API documentation is available here:

Sample Usage
------------
Here is a simple example of GSL ported to Java from this C source file:
Here is a simple example of GSL ported to Java from this demo.c source file:

* https://www.gnu.org/software/gsl/manual/html_node/An-Example-Program.html
* https://www.gnu.org/software/gsl/manual/html_node/Random-Number-Distribution-Examples.html

We can use [Maven 3](http://maven.apache.org/) to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the `pom.xml` and `src/main/java/Example.java` source files below, simply execute on the command line:
We can use [Maven 3](http://maven.apache.org/) to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the `pom.xml` and `src/main/java/Demo.java` source files below, simply execute on the command line:
```bash
$ mvn compile exec:java
```
Expand All @@ -33,10 +33,10 @@ We can use [Maven 3](http://maven.apache.org/) to download and install automatic
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.bytedeco.javacpp-presets.gsl</groupId>
<artifactId>example</artifactId>
<artifactId>demo</artifactId>
<version>1.4.3</version>
<properties>
<exec.mainClass>Example</exec.mainClass>
<exec.mainClass>Demo</exec.mainClass>
</properties>
<dependencies>
<dependency>
Expand All @@ -48,16 +48,39 @@ We can use [Maven 3](http://maven.apache.org/) to download and install automatic
</project>
```

### The `src/main/java/Example.java` source file
### The `src/main/java/Demo.java` source file
```java
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.gsl.*;

public class Example {
public class Demo {
public static void main(String[] args) {
double x = 5.0;
double y = gsl_sf_bessel_J0(x);
System.out.printf("J0(%g) = %.18e\n", x, y);
gsl_rng_type T;
gsl_rng r;

int n = 10;
double mu = 3.0;

/* create a generator chosen by the
environment variable GSL_RNG_TYPE */

gsl_rng_env_setup();

T = gsl_rng_default();
r = gsl_rng_alloc(T);

/* print n random variates chosen from
the poisson distribution with mean
parameter mu */

for (int i = 0; i < n; i++) {
int k = gsl_ran_poisson(r, mu);
System.out.printf(" %d", k);
}

System.out.println();
gsl_rng_free(r);
System.exit(0);
}
}
```

0 comments on commit 0cc6ee6

Please sign in to comment.