# AlgorithmStar 实现 向量 计算
*AlgorithmStar*
本文中将会演示通过 AS 机器学习库 实现 向量计算
## 目录
[TOC]

## 向量创建
在这里我们将学习如何创建一个向量对象
### 指定向量中的元素
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector vector1 = vectorFactory.parseVector(1, 2, 3, 4);
// 打印创建出来的向量对象
System.out.println(vector1);
}
}
```
在下面就是代码的运行结果
```
[ 1 2 3 4 ]
进程已结束,退出代码0
```
### 指定行列名字的创建向量
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.ColumnIntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final ColumnIntegerVector vector1 = (ColumnIntegerVector) vectorFactory.parseVector(
"行名",
new String[]{"列1", "列2", "列3", "列4"},
1, 2, 3, 4
);
// 打印创建出来的向量对象
System.out.println(vector1);
// 代表列名行名的向量创建 key 是向量中每个元素对应的名字,也是列名
System.out.println(vector1.toHashMap());
}
}
```
下面是代码的运行结果
```
[ 1 2 3 4 ]
{列2=2, 列1=1, 列4=4, 列3=3}
进程已结束,退出代码0
```
### 按照区间创建向量
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.FastRangeIntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final FastRangeIntegerVector vector1 = vectorFactory.parseRangeVector(1, 10);
// 打印创建出来的向量对象
System.out.println(vector1.toVector());
}
}
```
下面就是代码的运行结果
```
[ 1 2 3 4 5 6 7 8 9 10 ]
进程已结束,退出代码0
```
## 向量计算
### 向量的基本计算
#### 向量与向量或实数之间的加法
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector vector1 = vectorFactory.parseVector(
"行名",
new String[]{"列1", "列2", "列3", "列4"},
1, 2, 3, 4
);
final IntegerVector vector2 = vectorFactory.parseVector(10, 20, 30, 40);
// 计算出两个向量的求和
System.out.println(vector1.add(vector2));
// 计算出向量与实数之间的求和
System.out.println(vector1.add(10));
}
}
```
下面就是运行结果
```
[ 11 22 33 44 ]
[ 11 12 13 14 ]
进程已结束,退出代码0
```
#### 向量与向量或实数之间的减法
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector vector1 = vectorFactory.parseVector(
"行名",
new String[]{"列1", "列2", "列3", "列4"},
1, 2, 3, 4
);
final IntegerVector vector2 = vectorFactory.parseVector(10, 20, 30, 40);
// 计算出两个向量的求和
System.out.println(vector1.diff(vector2));
// 计算出向量与实数之间的求差
System.out.println(vector1.diff(10));
}
}
```
下面就是运行结果
```
[ -9 -18 -27 -36 ]
[ -9 -8 -7 -6 ]
进程已结束,退出代码0
```
#### 向量与向量之间的内积
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector vector1 = vectorFactory.parseVector(
"行名",
new String[]{"列1", "列2", "列3", "列4"},
1, 2, 3, 4
);
final IntegerVector vector2 = vectorFactory.parseVector(10, 20, 30, 40);
// 计算出两个向量的内积
System.out.println(vector1.innerProduct(vector2));
}
}
```
下面就是运行结果
```
300
进程已结束,退出代码0
```
#### 向量与向量之间的外积
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector vector1 = vectorFactory.parseVector(
"行名",
new String[]{"列1", "列2", "列3", "列4"},
1, 2, 3, 4
);
final IntegerVector vector2 = vectorFactory.parseVector(10, 20, 30, 40);
// 计算出两个向量的外积
System.out.println(vector1.multiply(vector2));
}
}
```
下面就是运行结果
```
[ 11 22 33 44 ]
[ 11 12 13 14 ]
进程已结束,退出代码0
```
### 向量的转换计算
#### 向量元素的位移
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector integerVector = vectorFactory.parseVector(1, 2, 43, 4);
// 向量整体左移 2 位
System.out.println(integerVector.leftShift(2, true));
// 向量整体右移 2 位
System.out.println(integerVector.rightShift(2, true));
// 使用不拷贝的方式整体左移 1 位 TODO 不拷贝时的转换操作更快,且所有操作将被记录
System.out.println(integerVector.leftShift(1, false));
// 这个时候 integerVector 的元素已经变化了 所以在这里我们再改变将会在 左移 1 位之后的结果上进行改变
// 所以就是左移 1 位后 右移 2 位
System.out.println(integerVector.rightShift(2, false));
}
}
```
下面就是转换结果
```
[ 43 4 0 0 ]
[ 0 0 1 2 ]
[ 43 4 0 0 ]
[ 0 0 43 4 ]
进程已结束,退出代码0
```
#### 向量洗牌 - 打乱向量
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector integerVector = vectorFactory.parseVector(1, 2, 43, 4);
// 向量整体随机打乱,随机种子使用 22
System.out.println(integerVector.shuffle(22));
}
}
```
下面就是打印出来的结果,可以看到向量中的数据已经被打乱了
```
[ 1 43 4 2 ]
进程已结束,退出代码0
```
#### 向量反转
```
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector integerVector = vectorFactory.parseVector(1, 2, 43, 4);
// 向量整体反转 这里的参数代表的就是是否需要在拷贝的新向量中进行转换 1.28 以及 1.28 之前的版本
// 打印出的字符串不太正确 1.29版本中会修复此问题
System.out.println(integerVector.reverseLR(false));
}
}
```
下面就是计算结果
```
[ 4 43 2 1 ]
进程已结束,退出代码0
```
#### 向量模长计算
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector integerVector = vectorFactory.parseVector(1, 2, 43, 4);
// 向量模长的计算
System.out.println(integerVector.moduleLength());
}
}
```
下面就是计算结果,值得一提的是,针对向量模长的计算函数调用,其时间复杂度是 O(1) 您可以重复的调用此函数
```
43
进程已结束,退出代码0
```
#### 向量维度获取
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector integerVector = vectorFactory.parseVector(1, 2, 43, 4);
// 向量维度的获取,此操作将获取到向量的维度数 也可以认为是向量的元素数量
System.out.println(integerVector.getNumberOfDimensions());
}
}
```
下面就是计算结果
```
4
进程已结束,退出代码0
```
#### 向量到数组的转换
```java
package com.zhao;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.core.VectorFactory;
import zhao.algorithmMagic.operands.vector.IntegerVector;
import java.util.Arrays;
public class MAIN {
public static void main(String[] args) {
// 创建向量对象
final VectorFactory vectorFactory = AlgorithmStar.vectorFactory();
final IntegerVector integerVector = vectorFactory.parseVector(1, 2, 43, 4);
// 向量到数组的转换 TODO 使用拷贝的方式获取 这样返回的数组是可以修改的
System.out.println(Arrays.toString(integerVector.copyToNewArray()));
// TODO 使用不拷贝的方式获取,这样返回的数据是不可修改的数组
System.out.println(Arrays.toString(integerVector.toArray()));
}
}
```
下面就是打印出来的结果
```
[1, 2, 43, 4]
[1, 2, 43, 4]
进程已结束,退出代码0
```
------
***操作记录***
作者:[algorithmStar](http://www.lingyuzhao.top//index.html?search=23 "algorithmStar")
操作时间:2024-01-22 19:02:55 星期一
事件描述备注:保存/发布
中国 天津
[](如果不需要此记录可以手动删除,每次保存都会自动的追加记录)