复数类Java实现.docx_第1页
复数类Java实现.docx_第2页
复数类Java实现.docx_第3页
复数类Java实现.docx_第4页
复数类Java实现.docx_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/* * 操作复数的类Complex * * 周长发编制 */package javaalgorithm.algorithm;/* * 操作复数的类Complex * author 周长发 * version 1.0 */public class Complex private double real = 0.0;/ 复数的实部private double imaginary = 0.0;/ 复数的虚部private double eps = 0.0; / 缺省精度/* * 基本构造函数 */public Complex() /* * 指定值构造函数 * * param dblX - 指定的实部 * param dblY - 指定的虚部 */public Complex(double dblX, double dblY)real = dblX;imaginary = dblY;/* * 拷贝构造函数 * * param other - 源复数 */public Complex(Complex other)real = other.real;imaginary = other.imaginary;/* * 根据a,b形式的字符串来构造复数,以a为复数的实部,b为复数的虚部 * * param s - a,b形式的字符串,a为复数的实部,b为复数的虚部 * param sDelim - a, b之间的分隔符 */public Complex(String s, String sDelim)setValue(s, sDelim);/* * 设置复数运算的精度 * * param newEps - 新的精度值 */public void setEps(double newEps)eps = newEps;/* * 取复数的精度值 * * return double型,复数的精度值 */public double getEps()return eps;/* * 指定复数的实部 * * param dblX - 复数的实部 */public void setReal(double dblX)real = dblX;/* * 指定复数的虚部 * * param dblY - 复数的虚部 */public void setImag(double dblY)imaginary = dblY;/* * 取复数的实部 * * return double 型,复数的实部 */public double getReal()return real;/* * 取复数的虚部 * * return double 型,复数的虚部 */public double getImag()return imaginary;/* * 指定复数的实部和虚部值 * * param real - 指定的实部 * param imag - 指定的虚部 */public void setValue(double real, double imag)setReal(real);setImag(imag);/* * 将a,b形式的字符串转化为复数,以a为复数的实部,b为复数的虚部 * * param s - a,b形式的字符串,a为复数的实部,b为复数的虚部 * param sDelim - a, b之间的分隔符 */public void setValue(String s, String sDelim)int nPos = s.indexOf(sDelim);if (nPos = -1)s = s.trim();real = Double.parseDouble(s);imaginary = 0;elseint nLen = s.length();String sLeft = s.substring(0, nPos);String sRight = s.substring(nPos+1, nLen);sLeft = sLeft.trim();sRight = sRight.trim();real = Double.parseDouble(sLeft);imaginary = Double.parseDouble(sRight);/* * 将复数转化为a+bj形式的字符串 * * return String 型,a+bj形式的字符串 */public String toString()String s;if (real != 0.0)if (imaginary 0)s = new Float(real).toString() + + + new Float(imaginary).toString() + j;else if (imaginary 0)s = new Float(imaginary).toString() + j;else if (imaginary 0)s = new Float(-1*imaginary).toString() + j;elses = new Float(real).toString();return s;/* * 比较两个复数是否相等 * * param cpxX - 用于比较的复数 * return boolean型,相等则为true,否则为false */public boolean equal(Complex cpxX)return Math.abs(real - cpxX.real) = eps & Math.abs(imaginary - cpxX.imaginary) = Math.abs(cpxX.imaginary) e = cpxX.imaginary / cpxX.real; f = cpxX.real + e * cpxX.imaginary; x = (real + imaginary * e) / f; y = (imaginary - real * e) / f; else e = cpxX.real / cpxX.imaginary; f = cpxX.imaginary + e * cpxX.real; x = (real * e + imaginary) / f; y = (imaginary * e - real) / f; return new Complex(x, y);/* * 计算复数的模 * * return double型,指定复数的模 */public double abs() / 求取实部和虚部的绝对值 double x = Math.abs(real); double y = Math.abs(imaginary); if (real = 0)return y; if (imaginary = 0)return x; / 计算模 if (x y) return (x * Math.sqrt(1 + (y / x) * (y / x); return (y * Math.sqrt(1 + (x / y) * (x / y);/* * 计算复数的根 * * param n - 待求根的根次 * param cpxR - Complex型数组,长度为n,返回复数的所有根 */public void root(int n, Complex cpxR)if (n1) return; double q = Math.atan2(imaginary, real); double r = Math.sqrt(real*real + imaginary*imaginary); if (r != 0) r = (1.0/n)*Math.log(r);r = Math.exp(r); for (int k=0; k 0) t = 1.5707963268; else t = -1.5707963268; else if (real 0) t = Math.atan2(imaginary, real); else if (imaginary = 0) t = Math.atan2(imaginary, real) + PI; else t = Math.atan2(imaginary, real) - PI; / 模的幂 r = Math.exp(dblW * Math.log(Math.sqrt(real * real + imaginary * imaginary); / 复数的实幂指数 return new Complex(r * Math.cos(dblW * t), r * Math.sin(dblW * t);/* * 计算复数的复幂指数 * * param cpxW - 待求复幂指数的幂次 * param n - 控制参数,默认值为0。当n=0时,求得的结果为复幂指数的主值 * return Complex型,复数的复幂指数值 */public Complex pow(Complex cpxW, int n)/ 常量final double PI = 3.14159265358979;/ 局部变量 double r, s, u, v; / 特殊值处理 if (real = 0) if (imaginary = 0)return new Complex(0, 0); s = 1.5707963268 * (Math.abs(imaginary) / imaginary + 4 * n); else s = 2 * PI * n + Math.atan2(imaginary, real); if (real 0) s = s + PI; else s = s - PI; / 求幂运算公式 r = 0.5 * Math.log(real * real + imaginary * imaginary); v = cpxW.real * r + cpxW.imaginary * s; u = Math.exp(cpxW.real * r - cpxW.imaginary * s); return new Complex(u * Math.cos(v), u * Math.sin(v);/* * 计算复数的自然对数 * * return Complex型,复数的自然对数值 */public Complex log()double p = Math.log(Math.sqrt(real*real + imaginary*imaginary); return new Complex(p, Math.atan2(imaginary, real);/* * 计算复数的正弦 * * return Complex型,复数的正弦值 */public Complex sin() int i; double x, y, y1, br, b1, b2; double c = new double6; / 切比雪夫公式的常数系数 c0 = 1.13031820798497; c1 = 0.04433684984866; c2 = 0.00054292631191; c3 = 0.00000319843646; c4 = 0.00000001103607; c5 = 0.00000000002498; y1 = Math.exp(imaginary); x = 0.5 * (y1 + 1 / y1); br = 0; if (Math.abs(imaginary) = 1) y = 0.5 * (y1 - 1 / y1); else b1 = 0; b2 = 0; y1 = 2 * (2 * imaginary * imaginary - 1); for (i = 5; i =0; -i) br = y1 * b1 - b2 - ci; if (i != 0) b2 = b1; b1 = br; y = imaginary * (br - b1); / 组合计算结果 x = x * Math.sin(real); y = y * Math.cos(real);return new Complex(x, y);/* * 计算复数的余弦 * * return Complex型,复数的余弦值 */public Complex cos() int i; double x, y, y1, br, b1, b2; double c = new double6; / 切比雪夫公式的常数系数 c0 = 1.13031820798497; c1 = 0.04433684984866; c2 = 0.00054292631191; c3 = 0.00000319843646; c4 = 0.00000001103607; c5 = 0.00000000002498; y1 = Math.exp(imaginary); x = 0.5 * (y1 + 1 / y1); br = 0; if (Math.abs(imaginary) = 1) y = 0.5 * (y1 - 1 / y1); else b1 = 0; b2 = 0; y1 = 2 * (2 * imaginary

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论