Ce code est pour faire une modélisation de l'ensemble C sous JAVA, avec quelque notions un peu avancée sur cet ensemble, tel que les fonctions usuelle généralisée à C et d'autres fonctions tel que la résolutions d'une équation polynomiale dans C, ainsi que la construction d'un "Ordre Total" dans C...
Ce code Contient 2 interfaces pour faciliter la tâche de lecture et pour bien vous guider dans la manipulation de cette classe...
Source / Exemple :
package ma.scupper.mathematics.analysis;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/**
*
public class Complex implements ComplexFunctions,Comparable{
private double realPart, imaginaryPart;
public Complex(){
this.realPart = 0;
this.imaginaryPart = 0;
}
public Complex(double r, double im){
this.realPart = r;
this.imaginaryPart = im;
}
public Complex(float r, float im){
this.realPart = r;
this.imaginaryPart = im;
}
public Complex(long r, long im){
this.realPart = r;
this.imaginaryPart = im;
}
public Complex(int r, int im){
this.realPart = r;
this.imaginaryPart = im;
}
public static Complex[] solveDeg2(double a, double b, double c){
Complex[] solutions =null;
double delta = Math.pow(b, 2) - 4*a*c;
if(a!=0){
if(delta>0){
solutions = new Complex[2];
solutions[0] = new Complex((-b - Math.sqrt(delta))/(2*a),0);
solutions[1] = new Complex((-b + Math.sqrt(delta))/(2*a),0);
}else{
if(delta == 0){
solutions = new Complex[1];
solutions[0] = new Complex(-b/(2*a),0);
}else{
solutions = new Complex[2];
solutions[0] = new Complex((-b/(2*a)),-Math.sqrt(Math.abs(delta))/(2*a));
solutions[1] = new Complex((-b/(2*a)),Math.sqrt(Math.abs(delta))/(2*a));
}
}
}else{
if(b!=0){
if(c!=0){
solutions = new Complex[1];
solutions[0] = new Complex(-c/b, 0);
}else{
solutions = new Complex[1];
solutions[0] = new Complex(0,0);
}
}else{
if(c==0){
return null;
}else{
throw new java.lang.ArithmeticException("No Solution ...");
}
}
}
return solutions ;
}
public static Complex[] solveDeg2(Complex a, Complex b, Complex c){
Complex[] solutions = new Complex[2];
double r2 = a.getR(), theta2 = a.getTheta(),
r1 = b.getR(), theta1 = b.getTheta(),
r0 = c.getR(), theta0 = c.getTheta();
Complex delta = (b.pow(2)).substract(a.multiply(c)).multiply(new Complex(4,0));
solutions[0] = ((b.negate()).substract(c.getRoots(2)[0])).divide(a.multiply(new Complex(2,0)));
solutions[1] = ((b.negate()).add(c.getRoots(2)[0])).divide(a.multiply(new Complex(2,0)));
return solutions ;
}
public static BigDecimal solveDichotomy(ArrayList<BigDecimal> ar, BigDecimal a, BigDecimal b, BigDecimal err){
boolean flag = true;
long compt = 0;
BigDecimal fp ,fm , aa , bb , mid = null , fa , fb ,fmid = null ;
aa = a;
bb = b;
while(flag){
mid = (aa.add(bb)).divide(BigDecimal.valueOf(2)); //(a+b)/2
fa = BigDecimal.ZERO;
fb = BigDecimal.ZERO;
fmid = BigDecimal.ZERO;
//Calculate f(a) and f(b) and f((a+b)/2)
for(int i = 0 ; i<ar.size() ; i++){
fa = fa.add(
(ar.get(i)).multiply(aa.pow(i))
);
fb = fb.add(
(ar.get(i)).multiply(bb.pow(i))
);
fmid = fmid.add(
(ar.get(i)).multiply(mid.pow(i))
);
}
// System.out.println("fa := "+fa+" fb := "+fb+" fmid := "+fmid+" aa := "+aa+" bb := "+bb+ " "+" mid := "+mid);
if(fa.compareTo(BigDecimal.ZERO)== 1 && fb.compareTo(BigDecimal.ZERO)== -1){
//f(a) >= 0 and f(b) <= 0
fp = fa;
fm = fb;
if(fmid.compareTo(BigDecimal.ZERO)==0){
return mid;
}
}else{
if(fa.compareTo(BigDecimal.ZERO)== -1 && fb.compareTo(BigDecimal.ZERO)== 1){
fp = fb;
fm = fa;
if(fmid.compareTo(BigDecimal.ZERO)==0){
return mid;
}
}else{
if(fa.compareTo(BigDecimal.ZERO)== 0){
return aa;
}else{
if(fb.compareTo(BigDecimal.ZERO)== 0){
return bb;
}else{
if(fmid.compareTo(BigDecimal.ZERO)==0){
return mid;
}else{
//Couldn't find a root beetween these
throw new java.lang.ExceptionInInitializerError("Couldn'st find a root beetween these interval borders ...");
}
}
}
}
}
if(fp.compareTo(fm.negate()) == 1){
bb = mid;
}else{
aa = mid;
}
if( (
(fp.subtract(fm)).abs()
).compareTo(err)==-1){
flag = false;
}
compt++;
}
System.out.println("Iteration := "+compt);
return mid;
}
@Override
public Complex add(Complex x) {
return new Complex(this.realPart+x.realPart,this.imaginaryPart+x.imaginaryPart);
}
@Override
public Complex negate() {
return new Complex(-this.realPart,-this.imaginaryPart);
}
@Override
public Complex substract(Complex x) {
return new Complex(this.realPart-x.realPart,this.imaginaryPart-x.imaginaryPart);
}
@Override
public Complex multiply(Complex X) {
double a = this.realPart , b = this.imaginaryPart ,
x = X.realPart , y = X.imaginaryPart;
return new Complex(a*x - b*y, x*b + a*y);
}
@Override
public Complex inverse() {
double a = this.realPart , b = this.imaginaryPart;
return new Complex(
a/(Math.pow(a, 2)+Math.pow(b, 2)),
-b/(Math.pow(a, 2)+Math.pow(b, 2))
);
}
@Override
public Complex divide(Complex x) {
return this.multiply(x.inverse());
}
@Override
public Complex conjugate() {
return new Complex(this.realPart,-this.imaginaryPart);
}
@Override
public Complex pow(double x) {
double r = this.getR() , theta = this.getTheta();
return new Complex(Math.pow(r, x) , x*theta);
}
@Override
public Complex pow(Complex z) {
double r = this.getR() , theta = this.getTheta() , x = z.realPart , y = z.imaginaryPart;
if(r!=0){
double R = Math.pow( Math.E , x * Math.log( r ) - (theta * y )) ,
THETA = y * Math.log( r ) + x * theta;
return new Complex(
R * Math.cos(THETA)
,
R * Math.sin(THETA)
);
}else{
return new Complex();
}
}
@Override
public double abs() {
return this.getR();
}
@Override
public double getRealPart() {
return this.realPart;
}
@Override
public double getImaginaryPart() {
return this.imaginaryPart;
}
@Override
public double getR() {
double a = this.realPart , b = this.imaginaryPart;
return Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
}
@Override
public double getTheta() {
double a = this.realPart , b = this.imaginaryPart;
return Math.acos(a/this.getR());
}
@Override
public Complex[] getRoots(int n) {
Complex[] roots = new Complex[n] ;
for(int i = 0; i<n; i++){
roots[i] = new Complex(Math.pow(this.getR(),1/n), (2*i+this.getTheta())/n);
}
return roots;
}
@Override
public boolean isReal() {
return this.imaginaryPart == 0;
}
@Override
public boolean isImaginary() {
return this.realPart == 0;
}
@Override
public Complex log() {
double r = this.getR() , theta = this.getTheta();
return new Complex(
Math.log(r),
theta
);
}
@Override
public Complex sqrt(){
return this.pow(1/2);
}
@Override
public Complex sin() {
return (($E.pow(this.multiply($I))).substract($E.pow(this.multiply($_I)))).divide($2.multiply($I));
}
@Override
public Complex cos() {
return (($E.pow(this.multiply($I))).add($E.pow(this.multiply($_I)))).divide($2);
}
@Override
public Complex tan() {
return ((($E.pow(this.multiply($I))).substract($E.pow(this.multiply($_I)))).divide($I)).divide((($E.pow(this.multiply($I))).add($E.pow(this.multiply($_I)))));
}
@Override
public Complex cotan() {
return (this.tan()).inverse();
}
@Override
public Complex sec() {
return (this.cos()).inverse();
}
@Override
public Complex csc() {
return (this.sin()).inverse();
}
@Override
public Complex asin() {
return $_I.multiply(
($I.multiply(this)).add(
($1.substract(this.pow($2))).sqrt()
)
);
}
@Override
public Complex acos() {
return $_I.multiply(
this.add(
((this.pow($2)).substract($1)).sqrt()
)
);
}
@Override
public Complex atan() {
return ($I.multiply($1$2)).multiply(
(($1.substract($I.multiply(this))).log()).substract(
($1.add($I.multiply(this))).log()
)
);
}
@Override
public Complex sinh() {
return $1$2.multiply(
($E.pow(this)).substract($E.pow(this.negate()))
);
}
@Override
public Complex cosh() {
return $1$2.multiply(
($E.pow(this)).add($E.pow(this.negate()))
);
}
@Override
public Complex tanh() {
return (this.sinh()).divide(this.cosh());
}
@Override
public Complex coth() {
return (this.tanh()).inverse();
}
@Override
public Complex sech() {
return (this.cosh()).inverse();
}
@Override
public Complex cosech() {
return (this.sinh()).inverse();
}
@Override
public Complex argsh() {
return (this.add(
($1.add(this.pow($2))).sqrt()
)).log()
;
}
@Override
public Complex argch() {
return (this.add(
($_1.add(this.pow($2))).sqrt()
)).log()
;
}
@Override
public Complex argth() {
return $1$2.multiply(
(
(
(this.add($1)).divide($1.substract(this))
).log()
)
);
}
@Override
public Complex argcoth() {
return $1$2.multiply(
(
(
(this.add($1)).divide(this.substract($1))
).log()
)
);
}
@Override
public String toString(StringStructure s){
if(s.equals(StringStructure.CARTESIAN)){
return this.realPart+"+i"+this.imaginaryPart;
}else{
if(s.equals(StringStructure.MOIVRE)){
return this.getR()+"(cos"+this.getTheta()+"+i*sin"+this.getTheta()+")";
}else{
return this.getR()+"e^i"+this.getTheta();
}
}
}
public static SortedSet<Complex> simpleTotalOrder(ArrayList<Complex> ar){
TreeSet<Complex> ts = new TreeSet<Complex> ();
ts.addAll(ar);
SortedSet<Complex> s = Collections.synchronizedSortedSet(ts);
return s;
}
@Override
public boolean equals(Complex x) {
if(this.realPart== x.getRealPart() && this.imaginaryPart == x.getImaginaryPart()){
return true;
}else{
return false;
}
}
@Override
public int compareTo(Object o) {
if (o == null) {
throw new java.lang.NullPointerException("Null number ...");
}
if (getClass() != o.getClass()) {
throw new java.lang.ClassFormatError("Cannot compare the uncomparable ...");
}
final Complex other = (Complex) o;
if(this.getRealPart()>other.getRealPart()){
return 1;
}else{
if(this.getRealPart()<other.getRealPart()){
return -1;
}else{
if(this.getImaginaryPart()>other.getImaginaryPart()){
return 1;
}else{
if(this.getImaginaryPart()<other.getImaginaryPart()){
return -1;
}else{
return 0;
}
}
}
}
}
public static void main(String[] args){
Complex z1 = new Complex(2,0);
Complex z2 = new Complex(6,0);
Complex z3 = new Complex(3,0);
Complex z4 = new Complex(3,0);
Complex z5 = new Complex(3,0);
ArrayList<Complex> ar = new ArrayList<Complex>();
ar.add(z1);
ar.add(z2);
ar.add(z3);
ar.add(z4);
ar.add(z5);
SortedSet<Complex> ord = simpleTotalOrder(ar);
Iterator it = ord.iterator();
do{
Complex z = ((Complex)it.next());
if(z.equals(z1)){
System.out.print("z1 ");
}
if(z.equals(z2)){
System.out.print("z2 ");
}
if(z.equals(z3)){
System.out.print("z3 ");
}
if(z.equals(z4)){
System.out.print("z4 ");
}
if(z.equals(z5)){
System.out.print("z5 ");
}
System.out.println();
// System.out.println(
// ((Complex)it.next()).toString(StringStructure.CARTESIAN)
// );
}while(it.hasNext());
// for(int i = 0 ; i< 1000 ; i++){
// System.out.println(z2.getRoots(1000)[i].toString(StringStructure.CARTESIAN));
// }
//x^7-14*x^6+84*x^5-280*x^4+560*x^3-672*x^2+448*x-128
BigDecimal a0 = BigDecimal.valueOf(-128);
BigDecimal a1 = BigDecimal.valueOf(448);
BigDecimal a2 = BigDecimal.valueOf(-672);
BigDecimal a3 = BigDecimal.valueOf(560);
BigDecimal a4 = BigDecimal.valueOf(-280);
BigDecimal a5 = BigDecimal.valueOf(84);
BigDecimal a6 = BigDecimal.valueOf(-14);
BigDecimal a7 = BigDecimal.valueOf(1);
BigDecimal a = BigDecimal.valueOf(1.5);
BigDecimal b = BigDecimal.valueOf(2.2);
// l'erreur de calcul
BigDecimal err = new BigDecimal("0.000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
+ "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001");
ArrayList<BigDecimal> arr = new ArrayList<BigDecimal>();
arr.add(a0);
arr.add(a1);
arr.add(a2);
arr.add(a3);
arr.add(a4);
arr.add(a5);
arr.add(a6);
arr.add(a7);
BigDecimal sol = solveDichotomy(arr,a,b,err);
System.out.println("The Solution is : "+sol.toString());
}
}
////////////////////////////////////////////////////////////////////////////////////
// //
// Les interfaces utilisées pour contenir les constantes et les fonctions //
// //
//://///////////////////:::::::::::://///////////////////////////:::::::::://///
package ma.scupper.mathematics.analysis;
/**
*
public interface ComplexConstants {
Complex $0 = new Complex();
Complex $E = new Complex(Math.E,0);
Complex $1 = new Complex(1,0);
Complex $_1 = new Complex(-1,0);
Complex $I = new Complex(0,1);
Complex $_I = new Complex(0,-1);
Complex $1$2 = new Complex(1/2,0);
Complex $2 = new Complex(2,0);
Complex $_2 = new Complex(-2,0);
Complex $J = new Complex(-1/2,Math.sqrt(3)/2);
Complex $J_ = new Complex(-1/2,-Math.sqrt(3)/2);
public enum StringStructure{
CARTESIAN,
POLAR,
MOIVRE
}
}
package ma.scupper.mathematics.analysis;
/**
*
public interface ComplexFunctions extends ComplexConstants{
public Complex add(Complex x);
public Complex substract(Complex x);
public Complex negate();
public Complex inverse();
public Complex multiply(Complex x);
public Complex divide(Complex x);
public Complex conjugate();
public Complex pow(double x);
public Complex pow(Complex z);
public double abs();
public double getRealPart();
public double getImaginaryPart();
public double getR();
public double getTheta();
public Complex[] getRoots(int n);
public boolean isReal();
public boolean isImaginary();
public Complex log();
public Complex sqrt();
public Complex sin();
public Complex cos();
public Complex tan();
public Complex cotan();
public Complex sec();
public Complex csc();
public Complex asin();
public Complex acos();
public Complex atan();
public Complex sinh();
public Complex cosh();
public Complex tanh();
public Complex coth();
public Complex sech();
public Complex cosech();
public Complex argsh();
public Complex argch();
public Complex argth();
public Complex argcoth();
public String toString(StringStructure s);
public boolean equals(Complex x);
}
Conclusion :
Comme résultat, on a :
run:
z1
z3 z4 z5
z2
Iteration := 536
The Solution is : 2.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000444551749897015496688542682854112019382504912292981558696718116593735121790773767527113848352457750043959117129792934742773337846709805885582337667321687024324015426177905880063486342088367656644668969772163862870062244967257291502954464365422917767900083285526356113871169451375992097177863336379971119871554890711694896707136624562119175951835359228425659239292144775390625
BUILD SUCCESSFUL (total time: 3 seconds)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.