001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.math;
006
007import java.util.Objects;
008import org.ejml.simple.SimpleMatrix;
009
010/** A class for constructing arbitrary RxC matrices. */
011public final class MatBuilder {
012  private MatBuilder() {
013    throw new UnsupportedOperationException("This is a utility class!");
014  }
015
016  /**
017   * Fills the matrix with the given data, encoded in row major form. (The matrix is filled row by
018   * row, left to right with the given data).
019   *
020   * @param rows The number of rows of the matrix.
021   * @param cols The number of columns of the matrix.
022   * @param data The data to fill the matrix with.
023   * @param <R> The number of rows of the matrix.
024   * @param <C> The number of columns of the matrix.
025   * @return The constructed matrix.
026   */
027  public static <R extends Num, C extends Num> Matrix<R, C> fill(
028      Nat<R> rows, Nat<C> cols, double... data) {
029    if (Objects.requireNonNull(data).length != rows.getNum() * cols.getNum()) {
030      throw new IllegalArgumentException(
031          "Invalid matrix data provided. Wanted "
032              + rows.getNum()
033              + " x "
034              + cols.getNum()
035              + " matrix, but got "
036              + data.length
037              + " elements");
038    }
039    return new Matrix<>(new SimpleMatrix(rows.getNum(), cols.getNum(), true, data));
040  }
041}