Multivariable Linear Regression….In Java Part 1

Matrices: The first step

public class Matrix {
private double[][] matrix;
private int height;
private int width;
public Matrix(double[][] values){
this.height = values.length;
this.width = values[0].length;
this.matrix = values
}
public Matrix scalarMultiplication(double scalar) {
double[][] arr = new double[this.getHeight()][this.getWidth()];

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = (double)(this.getElement(i, j) * scalar);
}
}


Matrix ans = new Matrix(arr);
return ans;
}

public Matrix scalarDivision(double scalar) {
double[][] arr = new double[this.getHeight()][this.getWidth()];

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = (double) (this.getElement(i, j) / scalar);
}
}


Matrix ans = new Matrix(arr);
return ans;
}
public Matrix addMatrix(Matrix b) {
if (b.getWidth() != this.getWidth() || b.getHeight() != this.getHeight()) {
System.out.println("Matrices are not the same dimensions");
return null;
}
double[][] arr = new double[this.getHeight()][this.getWidth()];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = this.getElement(i, j) + b.getElement(i, j);
}
}
Matrix ans = new Matrix(arr);
return ans;
}

public Matrix subtractMatrix(Matrix b) {
if (b.getWidth() != this.getWidth() || b.getHeight() != this.getHeight()) {
System.out.println("Matrices are not the same dimensions");
return null;
}
double[][] arr = new double[this.getHeight()][this.getWidth()];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = this.getElement(i, j) - b.getElement(i, j);
}
}
Matrix ans = new Matrix(arr);
return ans;
}
public Matrix multiplyMatrix(Matrix b) {
double[][] arr = new double[this.getHeight()][b.getWidth()];
for (int row = 0; row < this.getHeight(); row++) {
for (int col = 0; col < b.getWidth(); col++) {
//multiply rowth row with colth column
arr[row][col] = multiplyCells(this, b, row, col);
}
}
Matrix ans = new Matrix(arr);
return ans;
}

public double multiplyCells(Matrix a, Matrix b, int row, int col) {
double val = 0;
for (int i = 0; i < a.getWidth(); i++) {
val += a.getElement(row, i) * b.getElement(i, col);
}
return val;
}
public Matrix transpose() {
double[][] arr = new double[this.getWidth()][this.getHeight()];
for (int row = 0; row < this.getHeight(); row++) {
for (int col = 0; col < this.getWidth(); col++) {
arr[col][row] = this.getElement(row, col);
}
}
Matrix ans = new Matrix(arr);
return ans;
}

--

--

--

A high school student interested in cognitive science and programming

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Agile Philippines Meetup 2019.05

Bluzelle Weekly Update: Week of April 25, 2022

Test Engineers Who Know The Answers

Euler puzzles

Shield Strength! — Fixed!

Quick Tips For Beginners to Neptune DXP — SAP Edition

How to Create a Fully Functional E-commerce Website with Django

Getting started with kotlin

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arnav Kartikeya

Arnav Kartikeya

A high school student interested in cognitive science and programming

More from Medium

LeetCode Patterns Adventure 15 — Reverse Linked List

Data Structures in Python

Basic Data Structure and Algorithms in Python

Why Java isn’t the Best Language for Beginners