J-Libraries / numJ

Java library to fulfil the requirement of numpy in java
https://j-libraries.github.io/numJ/
Apache License 2.0
21 stars 4 forks source link

Updated README.md

NumJ - A Java Numerical Computing Library

NumJ is a Java library inspired by NumPy, providing support for multi-dimensional arrays and mathematical operations. It aims to bring the functionality of NumPy to Java, enabling efficient numerical computations and array manipulations.

Table of Contents

Features

Installation

NumJ is available via JitPack and can be included in your project using Gradle or Maven.

Gradle

Add the following to your build.gradle file:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    implementation 'com.github.J-Libraries:numJ:1.0.1'
}

Maven

Add the following to your pom.xml file:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.J-Libraries</groupId>
    <artifactId>numJ</artifactId>
    <version>1.0.1</version>
</dependency>

Usage

NDArray Class

The NDArray<T> class represents an N-dimensional array and provides various methods for array manipulations.

Initialization

Since the NDArray constructor is package-private, you can create an NDArray instance using the array method provided by the NumJ class.

// For a 2D array of integers
Integer[][] data = {{1, 2, 3}, {4, 5, 6}};
NumJ<Integer> numj = new NumJ<>();
NDArray<Integer> array = numj.array(data);

Attributes

Methods

Method Details:

flatten()

Flattens an N-dimensional array into a one-dimensional array.

NDArray<Integer> flatArray = array.flatten();
transpose()

Transposes the array by reversing its dimensions. For a 2D array, it swaps rows and columns.

NDArray<Integer> transposedArray = array.transpose();
reshape(int... newShape)

Reshapes the array to the new specified shape. The total number of elements must remain the same.

NDArray<Integer> reshapedArray = array.reshape(3, 2);

Note: Throws ShapeException if the new shape is incompatible.

NumJ Class

The NumJ<T> class provides utility methods for creating arrays and other numerical operations.

array Method

Creates an NDArray from the provided multi-dimensional array.

Usage Example:

NumJ<Integer> numj = new NumJ<>();
Integer[][] data = {{1, 2, 3}, {4, 5, 6}};
NDArray<Integer> array = numj.array(data);

Note: Since the NDArray constructor is package-private, you should use the array method to create instances of NDArray.

arange Method

Creates an array with evenly spaced values within a given interval.

Overloads:

  1. arange(int end): Creates an array from 0 to end - 1.
  2. arange(int start, int end): Creates an array from start to end - 1.
  3. arange(int start, int end, DType dType): Creates an array with the specified data type.
  4. arange(int start, int end, int skip, int[] shape): Creates an array with dynamic steps and specified shape.

Usage Examples:

// Creates [0, 1, 2, 3, 4]
NDArray<Integer> array1 = numj.arange(5);

// Creates [2, 3, 4, 5, 6]
NDArray<Integer> array2 = numj.arange(2, 7);

// Creates [1.0, 2.0, 3.0] with FLOAT32 data type
NumJ<Float> numjFloat = new NumJ<>();
NDArray<Float> array3 = numjFloat.arange(1, 4, DType.FLOAT32);

// Creates [0, 2, 4, 6] with a shape of 2x2
int[] shape = {2, 2};
NDArray<Integer> array4 = numj.arange(0, 8, 2, shape);

Arithmetic Operations

The NumJ class now supports basic arithmetic operations on arrays: addition, subtraction, multiplication, and division. Arrays must have compatible shapes for broadcasting.

Supported Operations:

Usage Examples:

NumJ<Integer> numj = new NumJ<>();
NDArray<Integer> arr1 = numj.arange(5); // [0, 1, 2, 3, 4]
NDArray<Integer> arr2 = numj.arange(5); // [0, 1, 2, 3, 4]

// Addition
NDArray<Integer> sum = numj.add(arr1, arr2);
sum.printArray(); // [0, 2, 4, 6, 8]

// Subtraction
NDArray<Integer> diff = numj.subtract(arr1, arr2);
diff.printArray(); // [0, 0, 0, 0, 0]

Supported Data Types

The DType enum defines the supported numerical data types for arrays:

public enum DType {
    FLOAT32,  // 32-bit floating-point (Float)
    FLOAT64,  // 64-bit floating-point (Double)
    INT8,     // 8-bit signed integer (Byte)
    INT16,    // 16-bit signed integer (Short)
    INT32,    // 32-bit signed integer (Integer)
    INT64     // 64-bit signed integer (Long)
}

Example:

NumJ<Double> numjDouble = new NumJ<>();
NDArray<Double> doubleArray = numjDouble.arange(0, 10, DType.FLOAT64);

Exception Handling

NumJ provides robust exception handling through the ShapeException class for operations involving array shapes and sizes.

Common Exceptions:

Example:

try {
    NDArray<Integer> reshapedArray = array.reshape(4, 2); // May throw ShapeException
} catch (ShapeException e) {
    e.printStackTrace();
}

Examples

Flattening an Array

Integer[][] data = {{1, 2}, {3, 4}};
NumJ<Integer> numj = new NumJ<>();
NDArray<Integer> array = numj.array(data);
NDArray<Integer> flatArray = array.flatten();
flatArray.printArray(); // Output: [1, 2, 3, 4]

Transposing an Array

Integer[][] data = {{1, 2, 3}, {4, 5, 6}};
NumJ<Integer> numj = new NumJ<>();
NDArray<Integer> array = numj.array(data);
NDArray<Integer> transposedArray = array.transpose();
transposedArray.printArray(); // Output: [[1, 4], [2, 5], [3, 6]]

Reshaping an Array

Integer[][] data = {{1, 2, 3}, {4, 5, 6}};
NumJ<Integer> numj = new NumJ<>();
NDArray<Integer> array = numj.array(data);
NDArray<Integer> reshapedArray = array.reshape(3, 2);
reshapedArray.printArray(); // Output: [[1, 2], [3, 4], [5, 6]]

Creating an Array with arange

NumJ<Integer> numj = new NumJ<>();

// Create an array from 0 to 9
NDArray<Integer> array = numj.arange(10);
array.printArray(); // Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Arithmetic Operations on Arrays

NumJ<Integer> numj = new NumJ<>();
NDArray<Integer> arr1 = numj.arange(5); // [0, 1, 2, 3, 4]
NDArray<Integer> arr2 = numj.arange(5); // [0, 1, 2, 3, 4]

// Element-wise addition
NDArray<Integer> sum = numj.add(arr1, arr2);
sum.printArray(); // [0, 2, 4, 6, 8]

// Element-wise multiplication
NDArray<Integer> product = numj.multiply(arr1, arr2);
product.printArray(); // [0, 1, 4, 9, 16]

Future Scope

NumJ is continuously evolving. The future scope includes implementing more features from NumPy to enhance the library's capabilities. Below is a detailed list of planned additions:

1. Advanced Array Creation Functions

2. Array Manipulation and Reshaping

3. Mathematical Operations

4. Linear Algebra

5. Random Number Generation

6. Broadcasting and Advanced Indexing

7. Statistical Functions

8. Sorting, Searching, and Counting

9. Input/Output Operations

10. Handling Missing Data

11. Bitwise Operations

12. Set Operations

13. Memory Management and Performance

14. Data Type Handling

Contributing

We welcome contributions to NumJ! Follow the steps below to get started:

Steps for Contribution:

  1. Fork the Repository:

  2. Select a Task:

    • Go to the NumJ Project Board to view open tasks or issues.
    • Choose an issue or task that interests you. You can pick any task that is unassigned.
  3. Clone Your Fork:

    • Clone your forked repository to your local machine.
      git clone https://github.com/YOUR_USERNAME/NumJ.git
      cd NumJ
  4. Work on the Task:

    • Create a new branch from main for your task:
      git checkout -b feature/task-name
    • Make your changes, commit them, and push to your fork:
      git add .
      git commit -m "Add description of the task you worked on"
      git push origin feature/task-name
  5. Create a Pull Request:

    • After pushing your changes, go to your repository on GitHub and create a pull request (PR) against the main branch of the original NumJ repository.
  6. Review Process:

    • Your PR will be reviewed by maintainers. Please be patient, and address any comments or requested changes.
  7. Merge:

    • Once approved, your PR will be merged into the main repository, and you will be credited for your contribution.

For more details on how to contribute, refer to the Contributing Guidelines or reach out to the maintainers.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contact Information