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 edu.wpi.first.math.numbers.N1;
008import edu.wpi.first.math.numbers.N10;
009import edu.wpi.first.math.numbers.N2;
010import edu.wpi.first.math.numbers.N3;
011import edu.wpi.first.math.numbers.N4;
012import edu.wpi.first.math.numbers.N5;
013import edu.wpi.first.math.numbers.N6;
014import edu.wpi.first.math.numbers.N7;
015import edu.wpi.first.math.numbers.N8;
016import edu.wpi.first.math.numbers.N9;
017import java.util.Objects;
018import org.ejml.simple.SimpleMatrix;
019
020/** A class for constructing vectors (Nx1 matrices). */
021public final class VecBuilder {
022  private VecBuilder() {
023    throw new UnsupportedOperationException("this is a utility class!");
024  }
025
026  private static <N extends Num> Vector<N> fillVec(Nat<N> rows, double... data) {
027    if (Objects.requireNonNull(data).length != rows.getNum()) {
028      throw new IllegalArgumentException(
029          "Invalid vector data provided. Wanted "
030              + rows.getNum()
031              + " vector, but got "
032              + data.length
033              + " elements");
034    }
035    return new Vector<>(new SimpleMatrix(data));
036  }
037
038  /**
039   * Returns a 1x1 vector containing the given elements.
040   *
041   * @param n1 the first element.
042   * @return 1x1 vector
043   */
044  public static Vector<N1> fill(double n1) {
045    return fillVec(Nat.N1(), n1);
046  }
047
048  /**
049   * Returns a 2x1 vector containing the given elements.
050   *
051   * @param n1 the first element.
052   * @param n2 the second element.
053   * @return 2x1 vector
054   */
055  public static Vector<N2> fill(double n1, double n2) {
056    return fillVec(Nat.N2(), n1, n2);
057  }
058
059  /**
060   * Returns a 3x1 vector containing the given elements.
061   *
062   * @param n1 the first element.
063   * @param n2 the second element.
064   * @param n3 the third element.
065   * @return 3x1 vector
066   */
067  public static Vector<N3> fill(double n1, double n2, double n3) {
068    return fillVec(Nat.N3(), n1, n2, n3);
069  }
070
071  /**
072   * Returns a 4x1 vector containing the given elements.
073   *
074   * @param n1 the first element.
075   * @param n2 the second element.
076   * @param n3 the third element.
077   * @param n4 the fourth element.
078   * @return 4x1 vector
079   */
080  public static Vector<N4> fill(double n1, double n2, double n3, double n4) {
081    return fillVec(Nat.N4(), n1, n2, n3, n4);
082  }
083
084  /**
085   * Returns a 5x1 vector containing the given elements.
086   *
087   * @param n1 the first element.
088   * @param n2 the second element.
089   * @param n3 the third element.
090   * @param n4 the fourth element.
091   * @param n5 the fifth element.
092   * @return 5x1 vector
093   */
094  public static Vector<N5> fill(double n1, double n2, double n3, double n4, double n5) {
095    return fillVec(Nat.N5(), n1, n2, n3, n4, n5);
096  }
097
098  /**
099   * Returns a 6x1 vector containing the given elements.
100   *
101   * @param n1 the first element.
102   * @param n2 the second element.
103   * @param n3 the third element.
104   * @param n4 the fourth element.
105   * @param n5 the fifth element.
106   * @param n6 the sixth element.
107   * @return 6x1 vector
108   */
109  public static Vector<N6> fill(double n1, double n2, double n3, double n4, double n5, double n6) {
110    return fillVec(Nat.N6(), n1, n2, n3, n4, n5, n6);
111  }
112
113  /**
114   * Returns a 7x1 vector containing the given elements.
115   *
116   * @param n1 the first element.
117   * @param n2 the second element.
118   * @param n3 the third element.
119   * @param n4 the fourth element.
120   * @param n5 the fifth element.
121   * @param n6 the sixth element.
122   * @param n7 the seventh element.
123   * @return 7x1 vector
124   */
125  public static Vector<N7> fill(
126      double n1, double n2, double n3, double n4, double n5, double n6, double n7) {
127    return fillVec(Nat.N7(), n1, n2, n3, n4, n5, n6, n7);
128  }
129
130  /**
131   * Returns a 8x1 vector containing the given elements.
132   *
133   * @param n1 the first element.
134   * @param n2 the second element.
135   * @param n3 the third element.
136   * @param n4 the fourth element.
137   * @param n5 the fifth element.
138   * @param n6 the sixth element.
139   * @param n7 the seventh element.
140   * @param n8 the eighth element.
141   * @return 8x1 vector
142   */
143  public static Vector<N8> fill(
144      double n1, double n2, double n3, double n4, double n5, double n6, double n7, double n8) {
145    return fillVec(Nat.N8(), n1, n2, n3, n4, n5, n6, n7, n8);
146  }
147
148  /**
149   * Returns a 9x1 vector containing the given elements.
150   *
151   * @param n1 the first element.
152   * @param n2 the second element.
153   * @param n3 the third element.
154   * @param n4 the fourth element.
155   * @param n5 the fifth element.
156   * @param n6 the sixth element.
157   * @param n7 the seventh element.
158   * @param n8 the eighth element.
159   * @param n9 the ninth element.
160   * @return 9x1 vector
161   */
162  public static Vector<N9> fill(
163      double n1,
164      double n2,
165      double n3,
166      double n4,
167      double n5,
168      double n6,
169      double n7,
170      double n8,
171      double n9) {
172    return fillVec(Nat.N9(), n1, n2, n3, n4, n5, n6, n7, n8, n9);
173  }
174
175  /**
176   * Returns a 10x1 vector containing the given elements.
177   *
178   * @param n1 the first element.
179   * @param n2 the second element.
180   * @param n3 the third element.
181   * @param n4 the fourth element.
182   * @param n5 the fifth element.
183   * @param n6 the sixth element.
184   * @param n7 the seventh element.
185   * @param n8 the eighth element.
186   * @param n9 the ninth element.
187   * @param n10 the tenth element.
188   * @return 10x1 vector
189   */
190  public static Vector<N10> fill(
191      double n1,
192      double n2,
193      double n3,
194      double n4,
195      double n5,
196      double n6,
197      double n7,
198      double n8,
199      double n9,
200      double n10) {
201    return fillVec(Nat.N10(), n1, n2, n3, n4, n5, n6, n7, n8, n9, n10);
202  }
203}