/*
 * Copyright 1997-2025 Optimatika
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.ojalgo.equation;

import static org.ojalgo.function.constant.PrimitiveMath.*;

import java.util.ArrayList;
import java.util.List;

import org.ojalgo.array.ArrayR064;
import org.ojalgo.array.BasicArray;
import org.ojalgo.array.DenseArray;
import org.ojalgo.array.PlainArray;
import org.ojalgo.array.SparseArray;
import org.ojalgo.function.UnaryFunction;
import org.ojalgo.structure.Access1D;
import org.ojalgo.structure.ElementView1D;
import org.ojalgo.structure.Mutate1D;
import org.ojalgo.type.NumberDefinition;

public final class Equation implements Comparable<Equation>, Access1D<Double>, Mutate1D.Modifiable<Double> {

    public static Equation dense(final int pivot, final int cols) {
        return Equation.dense(pivot, cols, ArrayR064.FACTORY);
    }

    public static <N extends Comparable<N>> Equation dense(final int pivot, final int cols, final DenseArray.Factory<N, ?> factory) {
        return new Equation(factory.make(cols), pivot, ZERO);
    }

    public static List<Equation> denseSystem(final int rows, final int cols) {
        return Equation.denseSystem(rows, cols, ArrayR064.FACTORY);
    }

    public static <N extends Comparable<N>> List<Equation> denseSystem(final int rows, final int cols, final DenseArray.Factory<N, ?> factory) {

        List<Equation> system = new ArrayList<>(rows);

        for (int i = 0; i < rows; i++) {
            system.add(new Equation(factory.make(cols), i, ZERO));
        }

        return system;
    }

    public static Equation of(final double rhs, final int pivot, final double... body) {
        return new Equation(ArrayR064.wrap(body), pivot, rhs);
    }

    public static Equation sparse(final int pivot, final int cols) {
        return Equation.sparse(pivot, cols, ArrayR064.FACTORY);
    }

    public static Equation sparse(final int pivot, final int cols, final int numberOfNonzeros) {
        return Equation.sparse(pivot, cols, ArrayR064.FACTORY, numberOfNonzeros);
    }

    public static <N extends Comparable<N>> Equation sparse(final int pivot, final int cols, final PlainArray.Factory<N, ?> factory) {
        return new Equation(SparseArray.factory(factory).make(cols), pivot, ZERO);
    }

    public static <N extends Comparable<N>> Equation sparse(final int pivot, final int cols, final PlainArray.Factory<N, ?> factory,
            final int numberOfNonzeros) {
        return new Equation(SparseArray.factory(factory).initial(numberOfNonzeros).make(cols), pivot, ZERO);
    }

    public static List<Equation> sparseSystem(final int rows, final int cols) {
        return Equation.sparseSystem(rows, cols, ArrayR064.FACTORY);
    }

    public static List<Equation> sparseSystem(final int rows, final int cols, final int numberOfNonzeros) {
        return Equation.sparseSystem(rows, cols, ArrayR064.FACTORY, numberOfNonzeros);
    }

    public static <N extends Comparable<N>> List<Equation> sparseSystem(final int rows, final int cols, final PlainArray.Factory<N, ?> factory) {

        List<Equation> system = new ArrayList<>(rows);

        for (int i = 0; i < rows; i++) {
            system.add(new Equation(SparseArray.factory(factory).make(cols), i, ZERO));
        }

        return system;
    }

    public static <N extends Comparable<N>> List<Equation> sparseSystem(final int rows, final int cols, final PlainArray.Factory<N, ?> factory,
            final int numberOfNonzeros) {

        List<Equation> system = new ArrayList<>(rows);

        for (int i = 0; i < rows; i++) {
            system.add(new Equation(SparseArray.factory(factory).initial(numberOfNonzeros).make(cols), i, ZERO));
        }

        return system;
    }

    public static Equation wrap(final BasicArray<?> body, final int pivot, final double rhs) {
        return new Equation(body, pivot, rhs);
    }

    /**
     * The row index of the original body matrix, [A].
     */
    public final int index;
    /**
     * The (nonzero) elements of this equation/row
     */
    private final BasicArray<?> myBody;
    private double myPivot = ZERO;
    private double myRHS;

    Equation(final BasicArray<?> body, final int pivot, final double rhs) {

        super();

        myBody = body;
        myRHS = rhs;

        index = pivot;
        myPivot = myBody.doubleValue(pivot);
    }

    @Override
    public void add(final int ind, final double addend) {
        myBody.add(ind, addend);
        if (ind == index) {
            myPivot = myBody.doubleValue(ind);
        }
    }

    @Override
    public void add(final long ind, final Comparable<?> addend) {
        this.add(ind, NumberDefinition.doubleValue(addend));
    }

    /**
     * Will perform a (relaxed) GaussSeidel update.
     * <p>
     * This call will not work if the pivot (the element at {@link #index}) is zero!
     *
     * @param x          The current solution (one element will be updated)
     * @param relaxation Typically 1.0 but could be anything (Most likely should be between 0.0 and 2.0).
     * @return The error in this equation
     */
    public <T extends Access1D<Double> & Mutate1D.Modifiable<Double>> double adjust(final T x, final double relaxation) {
        return this.calculate(x, myRHS, relaxation);
    }

    @Override
    public int compareTo(final Equation other) {
        return Integer.compare(index, other.index);
    }

    @Override
    public long count() {
        return myBody.count();
    }

    @Override
    public double dot(final Access1D<?> vector) {
        return myBody.dot(vector);
    }

    /**
     * Efficiently compute sum_{j < pivot} a_{ij} * x_j (excludes the pivot). Uses direct sparse iteration
     * when available to avoid O(n) random access scans.
     */
    public double dotLower(final Access1D<?> x) {

        double retVal = ZERO;

        if (myBody instanceof SparseArray) {

            for (ElementView1D<?, ?> nz : myBody.nonzeros()) {
                int j = (int) nz.index();
                if (j >= index) {
                    break;
                }
                retVal += nz.doubleValue() * x.doubleValue(j);
            }

        } else {

            for (int j = 0; j < index; j++) {
                double aij = myBody.doubleValue(j);
                if (aij != ZERO) {
                    retVal += aij * x.doubleValue(j);
                }
            }
        }

        return retVal;
    }

    /**
     * Efficiently compute sum_{j > pivot} a_{ij} * x_j (excludes the pivot). Uses direct sparse iteration
     * when available to avoid O(n) random access scans.
     */
    public double dotUpper(final Access1D<?> x) {

        double retVal = ZERO;

        if (myBody instanceof SparseArray) {

            for (ElementView1D<?, ?> nz : myBody.nonzeros()) {
                int j = (int) nz.index();
                if (j <= index) {
                    continue;
                }
                retVal += nz.doubleValue() * x.doubleValue(j);
            }

        } else {

            for (int j = index + 1, limit = myBody.size(); j < limit; j++) {
                double aij = myBody.doubleValue(j);
                if (aij != ZERO) {
                    retVal += aij * x.doubleValue(j);
                }
            }
        }

        return retVal;
    }

    @Override
    public double doubleValue(final int ind) {
        return myBody.doubleValue(ind);
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || !(obj instanceof Equation)) {
            return false;
        }
        final Equation other = (Equation) obj;
        if (index != other.index) {
            return false;
        }
        return true;
    }

    @Override
    public Double get(final long ind) {
        return Double.valueOf(myBody.doubleValue(ind));
    }

    public BasicArray<?> getBody() {
        return myBody;
    }

    public double[] getCoefficients() {
        return myBody.toRawCopy1D();
    }

    /**
     * @return The element at {@link #index}
     */
    public double getPivot() {
        return myPivot;
    }

    /**
     * @return The equation RHS
     */
    public double getRHS() {
        return myRHS;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        return prime * result + index;
    }

    /**
     * Initialises the solution vector's element at {@link #index} to the value that would solve this equation
     * if the RHS was zero and all other elements in the solution vector unchanged.
     *
     * @param <T> The solution vector type
     * @param x   The solution vector to be initialised
     */
    public <T extends Access1D<Double> & Mutate1D.Modifiable<Double>> void initialise(final T x) {
        this.calculate(x, ZERO, ONE);
    }

    @Override
    public void modifyOne(final long ind, final UnaryFunction<Double> modifier) {
        double value = myBody.doubleValue(ind);
        value = modifier.invoke(value);
        myBody.set(ind, value);
        if (ind == index) {
            myPivot = value;
        }
    }

    public void set(final long ind, final Comparable<?> value) {
        this.set(ind, NumberDefinition.doubleValue(value));
    }

    public void set(final long ind, final double value) {
        myBody.set(ind, value);
        if (ind == index) {
            myPivot = value;
        }
    }

    public void setRHS(final double rhs) {
        myRHS = rhs;
    }

    @Override
    public int size() {
        return myBody.size();
    }

    @Override
    public String toString() {
        return index + ": " + myBody.toString() + " = " + myRHS;
    }

    private <T extends Access1D<Double> & Mutate1D.Modifiable<Double>> double calculate(final T x, final double rhs, final double relaxation) {

        double increment = rhs;

        double error = increment -= myBody.dot(x);

        increment *= relaxation;

        increment /= myPivot;

        x.add(index, increment);

        return error;
    }

}
