Tuesday, 31 March 2020

TableSaw: Perform Arithmetic Operations between columns

TableSaw provides add, subtract, multiplication and division methods to perform Arithmetic operations between two columns.

Example
int[] evenNumbers = { 0, 2, 4, 6, 8, 10 };
int[] oddNumbers = {1, 3, 5, 7, 9, 11};

IntColumn evenNumbersColumn = IntColumn.create("Even Numbers", evenNumbers);
IntColumn oddNumbersColumn = IntColumn.create("Odd Numbers", oddNumbers);

DoubleColumn addition = evenNumbersColumn.add(oddNumbersColumn);
DoubleColumn subtraction = evenNumbersColumn.subtract(oddNumbersColumn);
DoubleColumn multiplication = evenNumbersColumn.multiply(oddNumbersColumn);
DoubleColumn division = evenNumbersColumn.divide(oddNumbersColumn);


App.java
package com.sample.app;

import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] evenNumbers = { 0, 2, 4, 6, 8, 10 };
  int[] oddNumbers = {1, 3, 5, 7, 9, 11};

  IntColumn evenNumbersColumn = IntColumn.create("Even Numbers", evenNumbers);
  IntColumn oddNumbersColumn = IntColumn.create("Odd Numbers", oddNumbers);

  DoubleColumn addition = evenNumbersColumn.add(oddNumbersColumn);
  DoubleColumn subtraction = evenNumbersColumn.subtract(oddNumbersColumn);
  DoubleColumn multiplication = evenNumbersColumn.multiply(oddNumbersColumn);
  DoubleColumn division = evenNumbersColumn.divide(oddNumbersColumn);
  
  System.out.println("Addition : "+ addition.print());
  System.out.println("Subtraction : "+ subtraction.print());
  System.out.println("Multiplication : "+ multiplication.print());
  System.out.println("Division : "+ division.print());
 }
}

Output

Addition : Column: Even Numbers + Odd Numbers
1.0
5.0
9.0
13.0
17.0
21.0

Subtraction : Column: Even Numbers - Odd Numbers
-1.0
-1.0
-1.0
-1.0
-1.0
-1.0

Multiplication : Column: Even Numbers * Odd Numbers
0.0
6.0
20.0
42.0
72.0
110.0

Division : Column: Even Numbers / Odd Numbers
0.0
0.6666666666666666
0.8
0.8571428571428571
0.8888888888888888
0.9090909090909091


Previous                                                    Next                                                    Home

No comments:

Post a Comment