Java向量的实现代码
时间: 2024-02-18 14:02:45 浏览: 25
Java中可以使用数组来表示向量,具体实现代码如下:
```java
public class Vector {
private double[] data;
// 构造函数
public Vector(double[] data) {
this.data = data;
}
// 向量加法
public Vector add(Vector other) {
double[] result = new double[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = data[i] + other.data[i];
}
return new Vector(result);
}
// 向量减法
public Vector subtract(Vector other) {
double[] result = new double[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = data[i] - other.data[i];
}
return new Vector(result);
}
// 向量点乘
public double dotProduct(Vector other) {
double sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i] * other.data[i];
}
return sum;
}
// 向量叉乘
public Vector crossProduct(Vector other) {
if (data.length != 3 || other.data.length != 3) {
throw new IllegalArgumentException("Cross product is only defined for 3D vectors.");
}
double[] result = new double[3];
result[0] = data[1] * other.data[2] - data[2] * other.data[1];
result[1] = data[2] * other.data[0] - data[0] * other.data[2];
result[2] = data[0] * other.data[1] - data[1] * other.data[0];
return new Vector(result);
}
// 获取向量长度
public double length() {
double sum = 0;
for (double d : data) {
sum += d * d;
}
return Math.sqrt(sum);
}
// 取负向量
public Vector negate() {
double[] result = new double[data.length];
for (int i = 0; i < data.length; i++) {
result[i] = -data[i];
}
return new Vector(result);
}
// 获取向量的字符串表示
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[ ");
for (double d : data) {
sb.append(d).append(" ");
}
sb.append("]");
return sb.toString();
}
}
```
使用示例:
```java
public static void main(String[] args) {
Vector a = new Vector(new double[] { 1, 2, 3 });
Vector b = new Vector(new double[] { 4, 5, 6 });
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a + b = " + a.add(b));
System.out.println("a - b = " + a.subtract(b));
System.out.println("a * b = " + a.dotProduct(b));
System.out.println("a x b = " + a.crossProduct(b));
System.out.println("|a| = " + a.length());
System.out.println("-a = " + a.negate());
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)