# AlgorithmStar 实现 复数 计算 *AlgorithmStar* 本文中将会演示通过 AS 机器学习库 实现 复数计算 ## 通知 > ⚠️【重要】 1.32 版本和 1.40 版本的内容几乎一致,主要的区别就是包模块的变更, 请注意,我们将在 1.40 版本以及之后的所有版本中 > 重构包名为 `io.github.beardedManZhao.algorithmStar` 这是为了避免在 Java 的诸多依赖中,包名出现冲突的情况~ > > 为了避免小伙伴们担心由于包更新导致的兼容性问题,因此我们提供了 1.32 > 版本,您可以继续使用旧包名,但是我们强烈建议您使用新版本,因为新版本的包名已经更新为 `io.github.beardedManZhao.algorithmStar` > ,若您对于修改包名称和更新有什么问题或建议,请及时联系我们!! ---- > 请注意哦 本文章是使用的旧版本的包名,若是新版本,您可以在 `io.github.beardedManZhao.algorithmStar` 中找到相关说明! ---- ## 目录 [TOC]  ## 创建复数 ### 指定实部与虚部 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse = complexNumberFactory.parse(1, 2); System.out.println(parse); } } ``` 下面就是计算结果 ``` 1.0 + 2.0i 进程已结束,退出代码0 ``` ### 通过复数表达式创建 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse = complexNumberFactory.parse("1 + 2i"); System.out.println(parse); } } ``` 下面就是计算结果 ``` 1.0 + 2.0i 进程已结束,退出代码0 ``` ## 复数计算 ### 复数的基本运算 #### 复数的加法 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse1 = complexNumberFactory.parse("1 + 2i"); final ComplexNumber parse2 = complexNumberFactory.parse("2 + 4i"); // 进行计算 System.out.println(parse1.add(parse2)); } } ``` 下面是计算结果 ``` 3.0 + 6.0i 进程已结束,退出代码0 ``` #### 复数的减法 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse1 = complexNumberFactory.parse("1 + 2i"); final ComplexNumber parse2 = complexNumberFactory.parse("2 + 4i"); // 进行计算 System.out.println(parse1.diff(parse2)); } } ``` 下面是计算结果 ``` -1.0 - 2.0i 进程已结束,退出代码0 ``` #### 复数的乘法 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse1 = complexNumberFactory.parse("1 + 2i"); final ComplexNumber parse2 = complexNumberFactory.parse("2 + 4i"); // 进行计算 System.out.println(parse1.multiply(parse2)); } } ``` 下面是计算结果 ``` -6.0 + 8.0i 进程已结束,退出代码0 ``` #### 复数的除法 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse1 = complexNumberFactory.parse("1 + 2i"); final ComplexNumber parse2 = complexNumberFactory.parse("2 + 4i"); // 进行计算 System.out.println(parse1.add(parse2)); } } ``` 下面是计算结果 ``` 0.5 + 0.0i 进程已结束,退出代码0 ``` ### 复数的转换计算 转换操作是基本运算操作中的一部分,在这里您将可以学习到诸多的计算操作 #### 复数的共轭 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse1 = complexNumberFactory.parse("1 + 2i"); // 转换为原本的共轭复数 System.out.println(parse1.conjugate()); } } ``` 下面就是计算出的结果。 ``` 1.0 - 2.0i 进程已结束,退出代码0 ``` #### 复数转换为实数 ``` package com.zhao; import zhao.algorithmMagic.core.AlgorithmStar; import zhao.algorithmMagic.core.ComplexNumberFactory; import zhao.algorithmMagic.operands.ComplexNumber; public class MAIN { public static void main(String[] args) { final ComplexNumberFactory complexNumberFactory = AlgorithmStar.complexNumberFactory(); // 创建复数对象 final ComplexNumber parse1 = complexNumberFactory.parse("1 + 0i"); // 针对虚部的值为0的复数,可以直接转换为实数 System.out.println(parse1.doubleValue()); } } ``` 下面就是计算出的结果 ``` 1.0 进程已结束,退出代码0 ``` ------ ***操作记录*** 作者:[algorithmStar](https://www.lingyuzhao.top//index.html?search=23 "algorithmStar") 操作时间:2024-06-21 17:31:25 星期五 事件描述备注:保存/发布 中国 天津 [](如果不需要此记录可以手动删除,每次保存都会自动的追加记录)