RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
Vector.java
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5package org.wpilib.math.linalg;
6
7import java.util.Objects;
8import org.ejml.simple.SimpleMatrix;
9import org.wpilib.math.linalg.proto.VectorProto;
10import org.wpilib.math.linalg.struct.VectorStruct;
11import org.wpilib.math.numbers.N1;
12import org.wpilib.math.numbers.N3;
13import org.wpilib.math.util.Nat;
14import org.wpilib.math.util.Num;
15import org.wpilib.util.protobuf.Protobuf;
16import org.wpilib.util.protobuf.ProtobufSerializable;
17import org.wpilib.util.struct.Struct;
18import org.wpilib.util.struct.StructSerializable;
19
27public class Vector<R extends Num> extends Matrix<R, N1>
34 public Vector(Nat<R> rows) {
35 super(rows, Nat.N1());
36 }
37
48 public Vector(SimpleMatrix storage) {
49 super(storage);
50 }
51
57 public Vector(Matrix<R, N1> other) {
58 super(other);
59 }
60
67 public double get(int row) {
68 return get(row, 0);
69 }
70
71 @Override
72 public Vector<R> times(double value) {
73 return new Vector<>(this.m_storage.scale(value));
74 }
75
76 @Override
77 public Vector<R> div(int value) {
78 return new Vector<>(this.m_storage.divide(value));
79 }
80
81 @Override
82 public Vector<R> div(double value) {
83 return new Vector<>(this.m_storage.divide(value));
84 }
85
92 public final Vector<R> plus(Vector<R> value) {
93 return new Vector<>(this.m_storage.plus(Objects.requireNonNull(value).m_storage));
94 }
95
102 public final Vector<R> minus(Vector<R> value) {
103 return new Vector<>(this.m_storage.minus(Objects.requireNonNull(value).m_storage));
104 }
105
112 public double dot(Vector<R> other) {
113 double dot = 0.0;
114
115 for (int i = 0; i < getNumRows(); ++i) {
116 dot += get(i, 0) * other.get(i, 0);
117 }
118
119 return dot;
120 }
121
127 public double norm() {
128 return Math.sqrt(dot(this));
129 }
130
136 public Vector<R> unit() {
137 return div(norm());
138 }
139
147 return other.times(dot(other)).div(other.dot(other));
148 }
149
157 public static Vector<N3> cross(Vector<N3> a, Vector<N3> b) {
158 return VecBuilder.fill(
159 a.get(1) * b.get(2) - a.get(2) * b.get(1),
160 a.get(2) * b.get(0) - a.get(0) * b.get(2),
161 a.get(0) * b.get(1) - a.get(1) * b.get(0));
162 }
163
171 public static final <R extends Num> VectorProto<R> getProto(Nat<R> rows) {
172 return new VectorProto<>(rows);
173 }
174
182 public static final <R extends Num> VectorStruct<R> getStruct(Nat<R> rows) {
183 return new VectorStruct<>(rows);
184 }
185}
Matrix(Nat< R > rows, Nat< C > columns)
Definition Matrix.java:46
static Vector< N1 > fill(double n1)
final Vector< R > plus(Vector< R > value)
Definition Vector.java:92
double dot(Vector< R > other)
Definition Vector.java:112
static final< R extends Num > VectorProto< R > getProto(Nat< R > rows)
Definition Vector.java:171
static Vector< N3 > cross(Vector< N3 > a, Vector< N3 > b)
Definition Vector.java:157
final Vector< R > minus(Vector< R > value)
Definition Vector.java:102
Vector(SimpleMatrix storage)
Definition Vector.java:48
Vector< R > projection(Vector< R > other)
Definition Vector.java:146
static final< R extends Num > VectorStruct< R > getStruct(Nat< R > rows)
Definition Vector.java:182
Vector(Matrix< R, N1 > other)
Definition Vector.java:57
static Nat< N1 > N1()
Definition Nat.java:63