diff --git a/src/com/javarush/module2/lesson02/baksalyar/CalcException.java b/src/com/javarush/module2/lesson02/baksalyar/CalcException.java new file mode 100644 index 0000000..f09f3e6 --- /dev/null +++ b/src/com/javarush/module2/lesson02/baksalyar/CalcException.java @@ -0,0 +1,23 @@ +package module2.lesson02.baksalyar; + +public class CalcException extends RuntimeException { + public CalcException(String message) { + super(message); + } + + protected CalcException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public CalcException(Throwable cause) { + super(cause); + } + + public CalcException(String message, Throwable cause) { + super(message, cause); + } + + public CalcException() { + super(); + } +} \ No newline at end of file diff --git a/src/com/javarush/module2/lesson02/baksalyar/MathRunner.java b/src/com/javarush/module2/lesson02/baksalyar/MathRunner.java new file mode 100644 index 0000000..8e44151 --- /dev/null +++ b/src/com/javarush/module2/lesson02/baksalyar/MathRunner.java @@ -0,0 +1,14 @@ +package module2.lesson02.baksalyar; + +import module2.lesson02.baksalyar.tensor.*; + +public class MathRunner { + static void main() { + Var left = new Scalar(1); +// Var right = new Scalar(2); +// Var right = new Vector(new double[]{1,2,3,4}); + Var right =new Matrix(new double[][]{{1, 2}, {3, 4}}); + var result = left.plus(right); + System.out.println(result); + } +} diff --git a/src/com/javarush/module2/lesson02/baksalyar/tensor/Matrix.java b/src/com/javarush/module2/lesson02/baksalyar/tensor/Matrix.java new file mode 100644 index 0000000..d4a05f4 --- /dev/null +++ b/src/com/javarush/module2/lesson02/baksalyar/tensor/Matrix.java @@ -0,0 +1,60 @@ +package module2.lesson02.baksalyar.tensor; + +import java.util.Arrays; + +public class Matrix extends Var { + private final double[][] values; + + public Matrix(double[][] values){this.values = values;} + + public Matrix(String strMatrixValues){ + String[] rows = strMatrixValues.trim().split("\\]\\s+\\["); + values = new double[rows.length][]; + + for (int i = 0; i < rows.length; i++) { + String row = rows[i]; + row = row.replace("[", "").replace("]", ""); + String[] elements = row.split(",\\s*"); + values[i] = new double[elements.length]; + for (int j = 0; j < elements.length; j++) { + values[i][j] = Integer.parseInt(elements[j].trim()); + } + } + } + + public double[][] getValues() { + return values; + } + + @Override + public Var plus(Var other) { + return other.plusMatrix(this); + } + + @Override + public Matrix plusScalar(Scalar other) { + double[][] result = values.clone(); + for (int i = 0; i < result.length; i++) { + for (int j = 0; j < result[i].length; j++) { + result[i][j] += other.getValue(); + } + } + return new Matrix(result); + } + + @Override + public Var plusMatrix(Matrix other) { + var result = other.values.clone(); + for (int i = 0; i < result.length; i++) { + for (int j = 0; j < result[i].length; j++) { + result[i][j] += values[i][j]; + } + } + return new Matrix(result); + } + + @Override + public String toString() { + return Arrays.deepToString(values); + } +} diff --git a/src/com/javarush/module2/lesson02/baksalyar/tensor/Scalar.java b/src/com/javarush/module2/lesson02/baksalyar/tensor/Scalar.java new file mode 100644 index 0000000..d4bbb12 --- /dev/null +++ b/src/com/javarush/module2/lesson02/baksalyar/tensor/Scalar.java @@ -0,0 +1,52 @@ +package module2.lesson02.baksalyar.tensor; + +public class Scalar extends Var { + private final double value; + + public Scalar(double value){ + this.value = value; + } + + public Scalar(String value){ + this.value = Double.parseDouble(value); + } + + public Scalar(Scalar other){this.value = other.value;} + + public double getValue() {return value;} + + @Override + public Var plus(Var other) { + return other.plusScalar(this); + } + + @Override + public Scalar plusScalar(Scalar other) { + return new Scalar(value + other.value); + } + + @Override + public Vector plusVector(Vector other) { + double[] result = other.getValues().clone(); + for(int i = 0; i < result.length; i++){ + result[i] += value; + } + return new Vector(result); + } + + @Override + public Matrix plusMatrix(Matrix other) { + double[][] result = other.getValues().clone(); + for (int i = 0; i < result.length; i++) { + for (int j = 0; j < result[i].length; j++) { + result[i][j] += value; + } + } + return new Matrix(result); + } + + @Override + public String toString() { + return value + ""; + } +} diff --git a/src/com/javarush/module2/lesson02/baksalyar/tensor/Var.java b/src/com/javarush/module2/lesson02/baksalyar/tensor/Var.java new file mode 100644 index 0000000..fef0032 --- /dev/null +++ b/src/com/javarush/module2/lesson02/baksalyar/tensor/Var.java @@ -0,0 +1,27 @@ +package module2.lesson02.baksalyar.tensor; + + +import module2.lesson02.baksalyar.CalcException; + +public abstract class Var { + public Var plus(Var other) {throw new CalcException("Operation %s + %s is not allowed".formatted(this, other));} + public Var plusScalar(Scalar other){throw new CalcException("Operation %s + %s is not allowed".formatted(this, other));} + public Var plusVector(Vector other){throw new CalcException("Operation %s + %s is not allowed".formatted(this, other));} + public Var plusMatrix(Matrix other){throw new CalcException("Operation %s + %s is not allowed".formatted(this, other));} + + public Var minus(Var other) {throw new CalcException("Operation %s - %s is not allowed".formatted(this, other));} + public Var minusScalar(Scalar other){throw new CalcException("Operation %s - %s is not allowed");} + public Var minusVector(Vector other){throw new CalcException("Operation %s - %s is not allowed");} + public Var minusMatrix(Matrix other){throw new CalcException("Operation %s - %s is not allowed");} + + public Var multiply(Var other) {throw new CalcException("Operation %s * %s is not allowed");} + public Var multiplyScalar(Scalar other){throw new CalcException("Operation %s * %s is not allowed");} + public Var multiplyVector(Vector other){throw new CalcException("Operation %s * %s is not allowed");} + public Var multiplyMatrix(Matrix other){throw new CalcException("Operation %s * %s is not allowed");} + + public Var divide(Var other) {throw new CalcException("Operation %s / %s is not allowed");} + public Var divisionScalar(Scalar other) {throw new CalcException("Operation %s / %s is not allowed");} + public Var divisionVector(Vector other) {throw new CalcException("Operation %s / %s is not allowed");} + public Var divisionMatrix(Matrix other) {throw new CalcException("Operation %s / %s is not allowed");} + +} diff --git a/src/com/javarush/module2/lesson02/baksalyar/tensor/Vector.java b/src/com/javarush/module2/lesson02/baksalyar/tensor/Vector.java new file mode 100644 index 0000000..05fa4e5 --- /dev/null +++ b/src/com/javarush/module2/lesson02/baksalyar/tensor/Vector.java @@ -0,0 +1,61 @@ +package module2.lesson02.baksalyar.tensor; + +import module2.lesson02.baksalyar.CalcException; + +import java.util.Arrays; + +public class Vector extends Var { + private final double[] values; + + public Vector(double[] values) { + this.values = values.clone(); + } + + public Vector(Vector vector) { + this.values = vector.values.clone(); + } + + public Vector(String strVectorValues){ + String[] parts = strVectorValues.replaceAll("\\s+", "") + .replace("[", "") + .replace("]", "") + .split(","); + values = new double[parts.length]; + for(int i = 0; i < strVectorValues.length(); i++){ + values[i] = Double.parseDouble(parts[i]); + } + } + + public double[] getValues() {return values;} + + @Override + public Var plus(Var other) { + return other.plusVector(this); + } + + @Override + public Vector plusScalar(Scalar other) { + double[] result = values.clone(); + for(int i = 0; i < result.length; i++){ + result[i] += other.getValue(); + } + return new Vector(result); + } + + @Override + public Vector plusVector(Vector other) { + if (values.length != other.values.length) { + throw new CalcException("Operation %s + %s is not allowed".formatted(this, other)); + } + var result = other.getValues().clone(); + for(int i = 0; i < result.length; i++){ + result[i] += values[i]; + } + return new Vector(result); + } + + @Override + public String toString() { + return Arrays.toString(values); + } +}