Commit e1a4eb86 authored by Kévin MIGADEL's avatar Kévin MIGADEL

Initial commit

parents
# Default ignored files
/workspace.xml
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_13" default="true" project-jdk-name="13" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Polymorphisme.iml" filepath="$PROJECT_DIR$/Polymorphisme.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
public class Cos extends Funct1 {
public Cos(Function function) {
super(function);
}
public double getValue() {
return Math.cos(super.function.getValue());
}
public Function getDiff(Var x) {
return new Mul(new Minus(new Val(0.0),function.getDiff(x)), new Sin(function));
}
public String toString() {
return "cos " + function.toString();
}
}
\ No newline at end of file
abstract class Funct0 implements Function {
abstract public double getValue();
}
abstract public class Funct1 implements Function {
protected Function function;
public Funct1(Function function) {
this.function = function;
}
abstract public double getValue();
}
abstract public class Funct2 implements Function {
protected Function function1;
protected Function function2;
public Funct2(Function function1, Function function2) {
this.function1 = function1;
this.function2 = function2;
}
abstract public double getValue();
}
import java.util.ArrayList;
import java.util.List;
abstract public class Funct3 implements Function {
protected List<Function> functionList;
public Funct3(Function ...list) {
functionList = new ArrayList<>();
for (Function function : list) {
functionList.add(function);
}
}
abstract public double getValue();
}
public interface Function {
public double getValue();
public Function getDiff(Var x);
}
public class Inv extends Funct1 {
public Inv(Function function) {
super(function);
}
public double getValue() {
return super.function.getValue()/1;
}
public Function getDiff(Var x) {
return new Minus(new Val(0.0), new Mul(function.getDiff(x), new Inv(new Sqr(function))));
}
public String toString() {
return "( 1/" + function.toString() + " )";
}
}
\ No newline at end of file
public class Minus extends Funct2 {
public Minus(Function function1, Function function2){
super(function1,function2);
}
public double getValue() {
return function1.getValue() - function2.getValue();
}
public Function getDiff(Var x) {
return new Val(0.0);
}
public String toString() {
return "(" + function1.toString() + "-" + function2.toString() + ")";
}
}
public class Mul extends Funct2 {
public Mul(Function function1, Function function2) {
super(function1, function2);
}
public double getValue() {
return function1.getValue() * function2.getValue();
}
public Function getDiff(Var x) {
return new Plus(new Mul(function1.getDiff(x),function2),new Mul(function1,function2.getDiff(x)));
}
public String toString() {
return "(" + function1.toString() + "*" + function2.toString() + ")";
}
}
public class Plus extends Funct2 {
public Plus(Function function1, Function function2) {
super(function1, function2);
}
public double getValue() {
return function1.getValue() + function2.getValue();
}
public Function getDiff(Var x) {
return new Plus(function1.getDiff(x), function2.getDiff(x));
}
public String toString() {
return "(" + function1.toString() + "+" + function2.toString() + ")";
}
}
import java.util.ArrayList;
import java.util.List;
public class PlusNAire extends Funct3 {
public PlusNAire(Function ...list) {
super(list);
}
public double getValue() {
double result = 0;
for(Function function : functionList) {
result += function.getValue();
}
return result;
}
public Function getDiff(Var x) {
Function[] functionListDiff = new Function[functionList.size()];
for (int cmp = 0; cmp < functionList.size(); cmp++) {
functionListDiff[cmp] = functionList.get(cmp).getDiff(x);
}
return new PlusNAire(functionListDiff);
}
public String toString() {
String result = "";
for (int cmp = 0; cmp < functionList.size(); cmp++) {
if(cmp == functionList.size() - 1) {
result += functionList.get(cmp).toString();
} else {
result += functionList.get(cmp).toString() + "+";
}
}
return "(" + result + ")";
}
}
public class Sin extends Funct1 {
public Sin(Function function) {
super(function);
}
public double getValue() {
return Math.sin(super.function.getValue());
}
public Function getDiff(Var x) {
return new Mul(function.getDiff(x), new Cos(function));
}
public String toString() {
return "sin " + function.toString();
}
}
public class Sqr extends Funct1 {
public Sqr(Function function) {
super(function);
}
public double getValue() {
return function.getValue()*function.getValue();
}
public Function getDiff(Var x) {
return new Mul(new Mul(new Val(2),function.getDiff(x)),function);
}
public String toString() {
return "(" + function.toString() + ")²";
}
}
public class Sqrt extends Funct1 {
public Sqrt(Function function) {
super(function);
}
public double getValue() {
return Math.sqrt(super.function.getValue());
}
public Function getDiff(Var x) {
return new Inv(new Mul(function.getDiff(x), new Sqrt(function)));
}
public String toString() {
return "sqrt " + function.toString();
}
}
\ No newline at end of file
public class Val extends Funct0 {
private double value;
public Val(double value) {
this.value = value;
}
public double getValue() {
return value;
}
public Function getDiff(Var x) {
return new Val(0.0);
}
public String toString() {
return Double.toString(value);
}
}
public class Var extends Funct0 {
private String name;
private double value = 0.0;
public Var(String name) {
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public Function getDiff(Var x) {
return new Val(1.0);
}
public String toString() {
return name;
}
}
public class main {
public static void main(String[] args) {
/* Function f
Var x = new Var("x");
Var y = new Var("y");
x.setValue(1.0);
y.setValue(1.0);
Function f = new Plus(x, new Mul(new Val(2), y));
System.out.println("Formule : " + f.toString());
System.out.println("Value : " + f.getValue());
*/
/* Function g
Var x = new Var("x");
Var y = new Var("y");
Var z = new Var("z");
x.setValue(2.0);
y.setValue(2.0);
z.setValue(2.0);
Function x2 = new Sqr(x);
Function y2 = new Sqr(y);
Function z2 = new Sqr(z);
Function g = new Plus(x2, new Plus(y2, z2));
System.out.println("Formule : " + g.toString());
System.out.println("Value : " + g.getValue());
*/
/* Function h
Var x = new Var("x");
x.setValue(0.0);
Function h = new Plus(new Sin(x), new Val(5));
System.out.println("Formule : " + h.toString());
System.out.println("Value : " + h.getValue());
*/
/* Function k
Var x = new Var("x");
Var y = new Var("y");
x.setValue(0.480000);
y.setValue(0.480000);
Function k = new Plus(new Sqr(new Sin(x)), new Sqr(new Cos(y)));
System.out.println("Formule : " + k.toString());
System.out.println("Value : " + k.getValue());
*/
/* Function j
Var x = new Var("x");
Var y = new Var("y");
Var z = new Var("z");
x.setValue(2.0);
y.setValue(2.0);
z.setValue(2.0);
Function x2 = new Sqr(x);
Function y2 = new Sqr(y);
Function z2 = new Sqr(z);
Function j=new PlusNAire(x2,y2,z2);
System.out.println("Formule : " + j.toString());
System.out.println("Value : " + j.getValue());
*/
/* Function l */
Var x = new Var("x");
Var y = new Var("y");
x.setValue(0.0);
y.setValue(0.0);
Function l = new Plus(new Sqr(new Sin(x)),new Sqr(new Cos(x)));
System.out.println("Formule : " + l.toString());
System.out.println("Formule Diff : " + l.getDiff(x).toString());
System.out.println("Value : " + l.getValue());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment