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 org.wpilib.math.linalg; 006 007import java.util.Objects; 008import org.ejml.simple.SimpleMatrix; 009import org.wpilib.math.util.Nat; 010import org.wpilib.math.util.Num; 011 012/** A class for constructing arbitrary RxC matrices. */ 013public final class MatBuilder { 014 private MatBuilder() { 015 throw new UnsupportedOperationException("This is a utility class!"); 016 } 017 018 /** 019 * Fills the matrix with the given data, encoded in row major form. (The matrix is filled row by 020 * row, left to right with the given data). 021 * 022 * @param rows The number of rows of the matrix. 023 * @param cols The number of columns of the matrix. 024 * @param data The data to fill the matrix with. 025 * @param <R> The number of rows of the matrix. 026 * @param <C> The number of columns of the matrix. 027 * @return The constructed matrix. 028 */ 029 public static <R extends Num, C extends Num> Matrix<R, C> fill( 030 Nat<R> rows, Nat<C> cols, double... data) { 031 if (Objects.requireNonNull(data).length != rows.getNum() * cols.getNum()) { 032 throw new IllegalArgumentException( 033 "Invalid matrix data provided. Wanted " 034 + rows.getNum() 035 + " x " 036 + cols.getNum() 037 + " matrix, but got " 038 + data.length 039 + " elements"); 040 } 041 return new Matrix<>(new SimpleMatrix(rows.getNum(), cols.getNum(), true, data)); 042 } 043}