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