Make first real commit: copy of CaRMetal 4.2.8
This commit is contained in:
parent
002acfc88e
commit
c312811084
1120 changed files with 226843 additions and 1 deletions
77
rene/zirkel/expression/ConvexMin.java
Normal file
77
rene/zirkel/expression/ConvexMin.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import rene.zirkel.construction.ConstructionException;
|
||||
import rene.zirkel.objects.Evaluator;
|
||||
|
||||
public class ConvexMin {
|
||||
public static double computeMin(final Evaluator F, double a, double b,
|
||||
final double eps) throws ConstructionException {
|
||||
final double lambda = (Math.sqrt(5) - 1) / 2;
|
||||
double x2 = lambda * a + (1 - lambda) * b;
|
||||
double y2 = F.evaluateF(x2);
|
||||
double x3 = (1 - lambda) * a + lambda * b;
|
||||
double y3 = F.evaluateF(x3);
|
||||
while (b - a > eps) {
|
||||
if (y2 < y3) {
|
||||
b = x3;
|
||||
x3 = x2;
|
||||
y3 = y2;
|
||||
x2 = lambda * a + (1 - lambda) * b;
|
||||
y2 = F.evaluateF(x2);
|
||||
} else {
|
||||
a = x2;
|
||||
x2 = x3;
|
||||
y2 = y3;
|
||||
x3 = (1 - lambda) * a + lambda * b;
|
||||
y3 = F.evaluateF(x3);
|
||||
}
|
||||
}
|
||||
return (a + b) / 2;
|
||||
}
|
||||
|
||||
public static double computeMax(final Evaluator F, double a, double b,
|
||||
final double eps) throws ConstructionException {
|
||||
final double lambda = (Math.sqrt(5) - 1) / 2;
|
||||
double x2 = lambda * a + (1 - lambda) * b;
|
||||
double y2 = F.evaluateF(x2);
|
||||
double x3 = (1 - lambda) * a + lambda * b;
|
||||
double y3 = F.evaluateF(x3);
|
||||
while (b - a > eps) {
|
||||
if (y2 > y3) {
|
||||
b = x3;
|
||||
x3 = x2;
|
||||
y3 = y2;
|
||||
x2 = lambda * a + (1 - lambda) * b;
|
||||
y2 = F.evaluateF(x2);
|
||||
} else {
|
||||
a = x2;
|
||||
x2 = x3;
|
||||
y2 = y3;
|
||||
x3 = (1 - lambda) * a + lambda * b;
|
||||
y3 = F.evaluateF(x3);
|
||||
}
|
||||
}
|
||||
return (a + b) / 2;
|
||||
}
|
||||
}
|
2784
rene/zirkel/expression/Expression.java
Normal file
2784
rene/zirkel/expression/Expression.java
Normal file
File diff suppressed because it is too large
Load diff
159
rene/zirkel/expression/ExpressionColor.java
Normal file
159
rene/zirkel/expression/ExpressionColor.java
Normal file
|
@ -0,0 +1,159 @@
|
|||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import rene.zirkel.construction.Construction;
|
||||
import rene.zirkel.construction.ConstructionException;
|
||||
import rene.zirkel.objects.ConstructionObject;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author erichake
|
||||
*/
|
||||
public class ExpressionColor {
|
||||
|
||||
private Construction C;
|
||||
private ConstructionObject O;
|
||||
private Expression R_EX=null, G_EX=null, B_EX=null;
|
||||
|
||||
public ExpressionColor(Construction c, ConstructionObject o) {
|
||||
C=c;
|
||||
O=o;
|
||||
}
|
||||
|
||||
public Color getColor() {
|
||||
if (R_EX==null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
int r=(int) R_EX.getValue();
|
||||
int g=(int) G_EX.getValue();
|
||||
int b=(int) B_EX.getValue();
|
||||
return new Color(r, g, b);
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setColor(Color col) {
|
||||
R_EX=null;
|
||||
G_EX=null;
|
||||
B_EX=null;
|
||||
if (col!=null) {
|
||||
R_EX=new Expression(""+col.getRed(), C, O);
|
||||
G_EX=new Expression(""+col.getGreen(), C, O);
|
||||
B_EX=new Expression(""+col.getBlue(), C, O);
|
||||
}
|
||||
}
|
||||
|
||||
public void setColor(int red, int green, int blue) {
|
||||
R_EX=null;
|
||||
G_EX=null;
|
||||
B_EX=null;
|
||||
R_EX=new Expression(""+red, C, O);
|
||||
G_EX=new Expression(""+green, C, O);
|
||||
B_EX=new Expression(""+blue, C, O);
|
||||
}
|
||||
|
||||
public void setColor(String red, String green, String blue) {
|
||||
R_EX=null;
|
||||
G_EX=null;
|
||||
B_EX=null;
|
||||
R_EX=new Expression(red, C, O);
|
||||
G_EX=new Expression(green, C, O);
|
||||
B_EX=new Expression(blue, C, O);
|
||||
}
|
||||
|
||||
public String getRedExpression() {
|
||||
if (R_EX==null) {
|
||||
return "";
|
||||
}
|
||||
return R_EX.toString();
|
||||
}
|
||||
public String getGreenExpression() {
|
||||
if (G_EX==null) {
|
||||
return "";
|
||||
}
|
||||
return G_EX.toString();
|
||||
}
|
||||
public String getBlueExpression() {
|
||||
if (B_EX==null) {
|
||||
return "";
|
||||
}
|
||||
return B_EX.toString();
|
||||
}
|
||||
|
||||
public void setRed(String s) throws ConstructionException {
|
||||
Expression red=new Expression(s, C, O);
|
||||
if (red.isValid()) {
|
||||
R_EX=null;
|
||||
R_EX=red;
|
||||
if (G_EX==null) G_EX=new Expression("0", C, O);
|
||||
if (B_EX==null) B_EX=new Expression("0", C, O);
|
||||
} else {
|
||||
throw new ConstructionException(red.getErrorText());
|
||||
}
|
||||
}
|
||||
|
||||
public void setGreen(String s) throws ConstructionException {
|
||||
Expression green=new Expression(s, C, O);
|
||||
if (green.isValid()) {
|
||||
G_EX=null;
|
||||
G_EX=green;
|
||||
if (R_EX==null) R_EX=new Expression("0", C, O);
|
||||
if (B_EX==null) B_EX=new Expression("0", C, O);
|
||||
} else {
|
||||
throw new ConstructionException(green.getErrorText());
|
||||
}
|
||||
}
|
||||
|
||||
public void setBlue(String s) throws ConstructionException {
|
||||
Expression blue=new Expression(s, C, O);
|
||||
if (blue.isValid()) {
|
||||
B_EX=null;
|
||||
B_EX=blue;
|
||||
if (G_EX==null) G_EX=new Expression("0", C, O);
|
||||
if (R_EX==null) R_EX=new Expression("0", C, O);
|
||||
} else {
|
||||
throw new ConstructionException(blue.getErrorText());
|
||||
}
|
||||
}
|
||||
|
||||
public int getRed() {
|
||||
if (R_EX==null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return (int) R_EX.getValue();
|
||||
} catch (Exception ex) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getGreen() {
|
||||
if (G_EX==null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return (int) G_EX.getValue();
|
||||
} catch (Exception ex) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getBlue() {
|
||||
if (B_EX==null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return (int) B_EX.getValue();
|
||||
} catch (Exception ex) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
245
rene/zirkel/expression/ExpressionString.java
Normal file
245
rene/zirkel/expression/ExpressionString.java
Normal file
|
@ -0,0 +1,245 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
import rene.util.MyVector;
|
||||
import rene.zirkel.objects.ConstructionObject;
|
||||
|
||||
class EquationExpressionString {
|
||||
public Expression E;
|
||||
|
||||
public EquationExpressionString(final Expression o) {
|
||||
E = o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return E.toString();
|
||||
}
|
||||
|
||||
public String getEquation() {
|
||||
final ConstructionObject o = E.getObject();
|
||||
if (o == null)
|
||||
return "???";
|
||||
else
|
||||
return o.getEquation();
|
||||
}
|
||||
|
||||
public void translate() {
|
||||
E.translate();
|
||||
}
|
||||
|
||||
public void addDep(final ConstructionObject o) {
|
||||
E.addDep(o);
|
||||
}
|
||||
}
|
||||
|
||||
class NameExpressionString {
|
||||
public Expression E;
|
||||
|
||||
public NameExpressionString(final Expression o) {
|
||||
E = o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return E.toString();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
final ConstructionObject o = E.getObject();
|
||||
if (o == null)
|
||||
return "???";
|
||||
else
|
||||
return o.getName();
|
||||
}
|
||||
|
||||
public void translate() {
|
||||
E.translate();
|
||||
}
|
||||
|
||||
public void addDep(final ConstructionObject o) {
|
||||
E.addDep(o);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class containing a chain of strings and expressions for alias and text lines.
|
||||
*/
|
||||
public class ExpressionString {
|
||||
MyVector v = new MyVector(3); // Strings or Expressions
|
||||
ConstructionObject O;
|
||||
|
||||
public ExpressionString(final String s, final ConstructionObject o) {
|
||||
O = o;
|
||||
v.removeAllElements();
|
||||
int h1 = 0;
|
||||
int n;
|
||||
while ((n = s.indexOf("%", h1)) >= 0) {
|
||||
final int h2 = s.indexOf("%", n + 1);
|
||||
if (h2 == n + 1) // found %%
|
||||
{
|
||||
v.addElement(s.substring(h1, n));
|
||||
v.addElement("%");
|
||||
h1 = n + 2;
|
||||
} else if (h2 >= 0) // found %...%
|
||||
{
|
||||
v.addElement(s.substring(h1, n));
|
||||
if (s.charAt(n + 1) == '~') // found %~...%
|
||||
{
|
||||
s.substring(n + 2, h2);
|
||||
v.addElement(new EquationExpressionString(new Expression(s
|
||||
.substring(n + 2, h2), O.getConstruction(), O)));
|
||||
h1 = h2 + 1;
|
||||
} else if (s.charAt(n + 1) == '=') // found %=...%
|
||||
{
|
||||
s.substring(n + 2, h2);
|
||||
v.addElement(new NameExpressionString(new Expression(s
|
||||
.substring(n + 2, h2), O.getConstruction(), O)));
|
||||
h1 = h2 + 1;
|
||||
} else {
|
||||
final Expression ex = new Expression(
|
||||
s.substring(n + 1, h2), O.getConstruction(), O);
|
||||
v.addElement(ex);
|
||||
h1 = h2 + 1;
|
||||
}
|
||||
} else // only one %
|
||||
{
|
||||
v.addElement(s.substring(h1, n));
|
||||
v.addElement("%");
|
||||
h1 = n + 1;
|
||||
}
|
||||
}
|
||||
if (!s.equals(""))
|
||||
v.addElement(s.substring(h1));
|
||||
// System.out.println(evaluate());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original string back (but all objects with correct current
|
||||
* names).
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
final Enumeration e = v.elements();
|
||||
while (e.hasMoreElements()) {
|
||||
final Object o = e.nextElement();
|
||||
if (o instanceof String) {
|
||||
if (((String) o).equals("%"))
|
||||
sb.append("%%");
|
||||
else
|
||||
sb.append((String) o);
|
||||
} else if (o instanceof Expression) {
|
||||
sb.append("%");
|
||||
if (((Expression) o).isForcePlus())
|
||||
sb.append("+");
|
||||
sb.append(o.toString());
|
||||
sb.append("%");
|
||||
} else if (o instanceof EquationExpressionString) {
|
||||
sb.append("%~");
|
||||
sb.append(((EquationExpressionString) o).toString());
|
||||
sb.append("%");
|
||||
} else if (o instanceof NameExpressionString) {
|
||||
sb.append("%=");
|
||||
sb.append(((NameExpressionString) o).toString());
|
||||
sb.append("%");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string with all expressions evaluated.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String evaluate() {
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
final Enumeration e = v.elements();
|
||||
while (e.hasMoreElements()) {
|
||||
final Object o = e.nextElement();
|
||||
if (o instanceof String) {
|
||||
if (((String) o).equals("%"))
|
||||
sb.append("%");
|
||||
else
|
||||
sb.append((String) o);
|
||||
} else if (o instanceof Expression) {
|
||||
try {
|
||||
double value = ((Expression) o).getValue();
|
||||
value = O.round(value);
|
||||
if (((Expression) o).isForcePlus() && value >= 0)
|
||||
sb.append("+");
|
||||
if (value == Math.floor(value + 0.5))
|
||||
sb.append((int) (value));
|
||||
else
|
||||
sb.append(O.round(value));
|
||||
} catch (final Exception exc) {
|
||||
sb.append("???");
|
||||
}
|
||||
} else if (o instanceof EquationExpressionString) {
|
||||
sb.append(((EquationExpressionString) o).getEquation());
|
||||
} else if (o instanceof NameExpressionString) {
|
||||
sb.append(((NameExpressionString) o).getName());
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the string for macros.
|
||||
*/
|
||||
public void translate() {
|
||||
final Enumeration e = v.elements();
|
||||
while (e.hasMoreElements()) {
|
||||
final Object o = e.nextElement();
|
||||
if (o instanceof Expression) {
|
||||
((Expression) o).translate();
|
||||
} else if (o instanceof EquationExpressionString) {
|
||||
((EquationExpressionString) o).translate();
|
||||
} else if (o instanceof NameExpressionString) {
|
||||
((NameExpressionString) o).translate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all objcts this string depends on to the DL of O.
|
||||
*
|
||||
* @param O
|
||||
*/
|
||||
public void addDep(final ConstructionObject oc) {
|
||||
final Enumeration e = v.elements();
|
||||
while (e.hasMoreElements()) {
|
||||
final Object o = e.nextElement();
|
||||
if (o instanceof Expression) {
|
||||
((Expression) o).addDep(oc);
|
||||
} else if (o instanceof EquationExpressionString) {
|
||||
((EquationExpressionString) o).addDep(oc);
|
||||
} else if (o instanceof NameExpressionString) {
|
||||
((NameExpressionString) o).addDep(oc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
35
rene/zirkel/expression/InvalidException.java
Normal file
35
rene/zirkel/expression/InvalidException.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import rene.zirkel.construction.ConstructionException;
|
||||
|
||||
public class InvalidException extends ConstructionException {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public InvalidException(final String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
41
rene/zirkel/expression/NoValueException.java
Normal file
41
rene/zirkel/expression/NoValueException.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import rene.zirkel.construction.ConstructionException;
|
||||
|
||||
public class NoValueException extends ConstructionException {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
boolean Valid;
|
||||
|
||||
public NoValueException(final boolean valid) {
|
||||
super("NoValueException");
|
||||
Valid = valid;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return Valid;
|
||||
}
|
||||
}
|
277
rene/zirkel/expression/Quartic.java
Normal file
277
rene/zirkel/expression/Quartic.java
Normal file
|
@ -0,0 +1,277 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
public class Quartic {
|
||||
|
||||
static public int solve(final double c[], final double sol[]) {
|
||||
double sum = 0;
|
||||
for (final double element : c)
|
||||
sum += Math.abs(element);
|
||||
if (sum < 1e-15) {
|
||||
sol[0] = 0;
|
||||
return 1;
|
||||
}
|
||||
for (int k = 0; k < c.length; k++)
|
||||
c[k] /= sum;
|
||||
int n = 4;
|
||||
while (Math.abs(c[n]) < 1e-15)
|
||||
n--;
|
||||
if (n == 4) {
|
||||
final double soli[] = new double[4];
|
||||
return quartic(c, sol, soli);
|
||||
} else if (n == 3) {
|
||||
return cubic(c, sol);
|
||||
} else if (n == 2) {
|
||||
final double a = -c[1] / (2 * c[2]);
|
||||
final double r = a * a - c[0] / c[2];
|
||||
if (r < -1e-10)
|
||||
return 0;
|
||||
else if (Math.abs(r) < 1e-10) {
|
||||
sol[0] = a;
|
||||
return 1;
|
||||
} else {
|
||||
sol[0] = a + Math.sqrt(r);
|
||||
sol[1] = a - Math.sqrt(r);
|
||||
return 2;
|
||||
}
|
||||
} else if (n == 1) {
|
||||
sol[0] = -c[0] / c[1];
|
||||
return 1;
|
||||
} else {
|
||||
if (Math.abs(c[0]) < 1e-10) {
|
||||
sol[0] = 0;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*-------------------- Global Function Description Block ----------------------
|
||||
*
|
||||
* ***QUARTIC************************************************25.03.98
|
||||
* Solution of a quartic equation
|
||||
* ref.: J. E. Hacke, Amer. Math. Monthly, Vol. 48, 327-328, (1941)
|
||||
* NO WARRANTY, ALWAYS TEST THIS SUBROUTINE AFTER DOWNLOADING
|
||||
* ******************************************************************
|
||||
* dd(0:4) (i) vector containing the polynomial coefficients
|
||||
* sol(1:4) (o) results, real part
|
||||
* soli(1:4) (o) results, imaginary part
|
||||
* Nsol (o) number of real solutions
|
||||
* ==================================================================
|
||||
* 17-Oct-2004 / Raoul Rausch
|
||||
* Conversion from Fortran to C
|
||||
*
|
||||
*
|
||||
*-----------------------------------------------------------------------------
|
||||
*/
|
||||
static int quartic(final double dd[], final double sol[],
|
||||
final double soli[]) {
|
||||
final double AA[] = new double[4], z[] = new double[3];
|
||||
double a, b, c, d, f, p, q, r, zsol, xK2, xL, xK, sqp, sqm;
|
||||
int ncube, i;
|
||||
int Nsol = 0;
|
||||
|
||||
if (dd[4] == 0.0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
a = dd[4];
|
||||
b = dd[3];
|
||||
c = dd[2];
|
||||
d = dd[1];
|
||||
f = dd[0];
|
||||
|
||||
p = (-3.0 * Math.pow(b, 2) + 8.0 * a * c) / (8.0 * Math.pow(a, 2));
|
||||
q = (Math.pow(b, 3) - 4.0 * a * b * c + 8.0 * d * Math.pow(a, 2))
|
||||
/ (8.0 * Math.pow(a, 3));
|
||||
r = (-3.0 * Math.pow(b, 4) + 16.0 * a * Math.pow(b, 2) * c - 64.0
|
||||
* Math.pow(a, 2) * b * d + 256.0 * Math.pow(a, 3) * f)
|
||||
/ (256.0 * Math.pow(a, 4));
|
||||
|
||||
// Solve cubic resolvent
|
||||
AA[3] = 8.0;
|
||||
AA[2] = -4.0 * p;
|
||||
AA[1] = -8.0 * r;
|
||||
AA[0] = 4.0 * p * r - Math.pow(q, 2);
|
||||
|
||||
ncube = cubic(AA, z);
|
||||
|
||||
zsol = -1.e99;
|
||||
for (i = 0; i < ncube; i++)
|
||||
zsol = Math.max(zsol, z[i]); // Not sure C has max fct
|
||||
z[0] = zsol;
|
||||
xK2 = 2.0 * z[0] - p;
|
||||
xK = Math.sqrt(xK2);
|
||||
xL = q / (2.0 * xK);
|
||||
sqp = xK2 - 4.0 * (z[0] + xL);
|
||||
sqm = xK2 - 4.0 * (z[0] - xL);
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
soli[i] = 0.0;
|
||||
if ((sqp >= 0.0) && (sqm >= 0.0)) {
|
||||
sol[0] = 0.5 * (xK + Math.sqrt(sqp));
|
||||
sol[1] = 0.5 * (xK - Math.sqrt(sqp));
|
||||
sol[2] = 0.5 * (-xK + Math.sqrt(sqm));
|
||||
sol[3] = 0.5 * (-xK - Math.sqrt(sqm));
|
||||
Nsol = 4;
|
||||
} else if ((sqp >= 0.0) && (sqm < 0.0)) {
|
||||
sol[0] = 0.5 * (xK + Math.sqrt(sqp));
|
||||
sol[1] = 0.5 * (xK - Math.sqrt(sqp));
|
||||
sol[2] = -0.5 * xK;
|
||||
sol[3] = -0.5 * xK;
|
||||
soli[2] = Math.sqrt(-.25 * sqm);
|
||||
soli[3] = -Math.sqrt(-.25 * sqm);
|
||||
Nsol = 2;
|
||||
} else if ((sqp < 0.0) && (sqm >= 0.0)) {
|
||||
sol[0] = 0.5 * (-xK + Math.sqrt(sqm));
|
||||
sol[1] = 0.5 * (-xK - Math.sqrt(sqm));
|
||||
sol[2] = 0.5 * xK;
|
||||
sol[3] = 0.5 * xK;
|
||||
soli[2] = Math.sqrt(-0.25 * sqp);
|
||||
soli[3] = -Math.sqrt(-0.25 * sqp);
|
||||
Nsol = 2;
|
||||
} else if ((sqp < 0.0) && (sqm < 0.0)) {
|
||||
sol[0] = -0.5 * xK;
|
||||
sol[1] = -0.5 * xK;
|
||||
soli[0] = Math.sqrt(-0.25 * sqm);
|
||||
soli[1] = -Math.sqrt(-0.25 * sqm);
|
||||
sol[2] = 0.5 * xK;
|
||||
sol[3] = 0.5 * xK;
|
||||
soli[2] = Math.sqrt(-0.25 * sqp);
|
||||
soli[3] = -Math.sqrt(-0.25 * sqp);
|
||||
Nsol = 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
sol[i] -= b / (4.0 * a);
|
||||
return Nsol;
|
||||
|
||||
}
|
||||
|
||||
/*-------------------- Global Function Description Block ----------------------
|
||||
*
|
||||
* ***CUBIC************************************************08.11.1986
|
||||
* Solution of a cubic equation
|
||||
* Equations of lesser degree are solved by the appropriate formulas.
|
||||
* The solutions are arranged in ascending order.
|
||||
* NO WARRANTY, ALWAYS TEST THIS SUBROUTINE AFTER DOWNLOADING
|
||||
* ******************************************************************
|
||||
* A(0:3) (i) vector containing the polynomial coefficients
|
||||
* X(1:L) (o) results
|
||||
* L (o) number of valid solutions (beginning with X(1))
|
||||
* ==================================================================
|
||||
* 17-Oct-2004 / Raoul Rausch
|
||||
* Conversion from Fortran to C
|
||||
*
|
||||
*
|
||||
*-----------------------------------------------------------------------------
|
||||
*/
|
||||
static public int cubic(final double A[], final double X[]) {
|
||||
final double PI = 3.1415926535897932;
|
||||
final double THIRD = 1. / 3.;
|
||||
final double U[] = new double[3];
|
||||
double W, P, Q, DIS, PHI;
|
||||
int i;
|
||||
int L;
|
||||
|
||||
if (A[3] != 0.0) {
|
||||
// cubic problem
|
||||
W = A[2] / A[3] * THIRD;
|
||||
P = Math.pow((A[1] / A[3] * THIRD - Math.pow(W, 2)), 3);
|
||||
Q = -.5 * (2.0 * Math.pow(W, 3) - (A[1] * W - A[0]) / A[3]);
|
||||
DIS = Math.pow(Q, 2) + P;
|
||||
if (DIS < 0.0) {
|
||||
// three real solutions!
|
||||
// Confine the argument of ACOS to the interval [-1;1]!
|
||||
PHI = Math.acos(Math
|
||||
.min(1.0, Math.max(-1.0, Q / Math.sqrt(-P))));
|
||||
P = 2.0 * Math.pow((-P), (5.e-1 * THIRD));
|
||||
for (i = 0; i < 3; i++)
|
||||
U[i] = P * Math.cos((PHI + 2 * ((double) i) * PI) * THIRD)
|
||||
- W;
|
||||
X[0] = Math.min(U[0], Math.min(U[1], U[2]));
|
||||
X[1] = Math.max(Math.min(U[0], U[1]), Math.max(Math.min(U[0],
|
||||
U[2]), Math.min(U[1], U[2])));
|
||||
X[2] = Math.max(U[0], Math.max(U[1], U[2]));
|
||||
L = 3;
|
||||
} else {
|
||||
// only one real solution!
|
||||
DIS = Math.sqrt(DIS);
|
||||
X[0] = CBRT(Q + DIS) + CBRT(Q - DIS) - W;
|
||||
L = 1;
|
||||
}
|
||||
} else if (A[2] != 0.0) {
|
||||
// quadratic problem
|
||||
P = 0.5 * A[1] / A[2];
|
||||
DIS = Math.pow(P, 2) - A[0] / A[2];
|
||||
if (DIS > 0.0) {
|
||||
// 2 real solutions
|
||||
X[0] = -P - Math.sqrt(DIS);
|
||||
X[1] = -P + Math.sqrt(DIS);
|
||||
L = 2;
|
||||
} else {
|
||||
// no real solution
|
||||
L = 0;
|
||||
}
|
||||
} else if (A[1] != 0.0) {
|
||||
// linear equation
|
||||
X[0] = A[0] / A[1];
|
||||
L = 1;
|
||||
} else {
|
||||
// no equation
|
||||
L = 0;
|
||||
}
|
||||
/*
|
||||
* ==== perform one step of a newton iteration in order to minimize
|
||||
* round-off errors ====
|
||||
*/
|
||||
for (i = 0; i < L; i++) {
|
||||
X[i] = X[i] - (A[0] + X[i] * (A[1] + X[i] * (A[2] + X[i] * A[3])))
|
||||
/ (A[1] + X[i] * (2.0 * A[2] + X[i] * 3.0 * A[3]));
|
||||
}
|
||||
|
||||
return L;
|
||||
}
|
||||
|
||||
static double CBRT(final double Z) {
|
||||
final double THIRD = 1. / 3.;
|
||||
if (Z > 0)
|
||||
return Math.pow(Z, THIRD);
|
||||
else if (Z < 0)
|
||||
return -Math.pow(Math.abs(Z), THIRD);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(final String[] args) {
|
||||
final double a[] = { 0, 3, -3, -6, 5 }, x[] = new double[5], y[] = new double[5];
|
||||
final int n = quartic(a, x, y);
|
||||
System.out.println(n + " solutions!");
|
||||
for (int i = 0; i < n; i++)
|
||||
System.out.println(x[i]);
|
||||
}
|
||||
|
||||
}
|
100
rene/zirkel/expression/Romberg.java
Normal file
100
rene/zirkel/expression/Romberg.java
Normal file
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import rene.zirkel.construction.ConstructionException;
|
||||
import rene.zirkel.objects.Evaluator;
|
||||
|
||||
public class Romberg {
|
||||
/**
|
||||
* Summiere f(x),f(x+h),...,f(x+n*h)
|
||||
*/
|
||||
private static double sumUp(final Evaluator F, final double x,
|
||||
final double h, final int n) throws ConstructionException {
|
||||
double sum = F.evaluateF(x);
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
sum = sum + F.evaluateF(x + i * h);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Romberg-Verfahren.
|
||||
*
|
||||
* @param a
|
||||
* ,b = Intervall-Grenzen
|
||||
* @param F
|
||||
* = Funktion, die integriert wird
|
||||
* @param nstart
|
||||
* = Anzahl der ersten Unterteilungen
|
||||
* @param eps
|
||||
* = Relative Genauigkeit zum Abbruch
|
||||
* @param maxiter
|
||||
* = Maximale Iterationen
|
||||
* @return Integral oder RuntimeException
|
||||
*/
|
||||
static public double compute(final Evaluator F, final double a,
|
||||
final double b, final int nstart, final double eps,
|
||||
final int maxiter) throws ConstructionException {
|
||||
// Ergebnisse der Trapezregel mit Schrittweite h/2^i
|
||||
final double t[] = new double[maxiter];
|
||||
int n = nstart;
|
||||
|
||||
// Anfangsschrittweite
|
||||
double h = (b - a) / n;
|
||||
// Berechne Trapezregel dazu und d[0]
|
||||
double tlast = t[0] = (F.evaluateF(a) + F.evaluateF(b) + 2 * sumUp(F, a
|
||||
+ h, h, n - 2))
|
||||
* h / 2;
|
||||
// Bisheriges Ergebnis festhalten
|
||||
double old = t[0];
|
||||
|
||||
// Halbiere Schrittweite bis Genauigkeit erreicht
|
||||
for (int i = 1; i < maxiter; i++) { // Halbiere Schrittweite:
|
||||
h = h / 2;
|
||||
n = n * 2;
|
||||
|
||||
// Berechne Trapezregel (unter Verwendung des
|
||||
// letzten Ergebnisses:
|
||||
t[i] = tlast / 2 + sumUp(F, a + h, 2 * h, n / 2 - 1) * h;
|
||||
tlast = t[i];
|
||||
|
||||
// Update der t[i]:
|
||||
double q = 4;
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
t[j] = t[j + 1] + (t[j + 1] - t[j]) / (q - 1);
|
||||
q = q * 4;
|
||||
}
|
||||
|
||||
// Abbruch-Kriterium
|
||||
final double res = t[0];
|
||||
if (Math.abs((res - old) / res) < eps)
|
||||
return res;
|
||||
old = res;
|
||||
}
|
||||
|
||||
// Bei ?berschreiten der Interationsgrenze:
|
||||
return tlast;
|
||||
}
|
||||
|
||||
}
|
64
rene/zirkel/expression/Secant.java
Normal file
64
rene/zirkel/expression/Secant.java
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import rene.zirkel.construction.ConstructionException;
|
||||
import rene.zirkel.objects.Evaluator;
|
||||
|
||||
public class Secant {
|
||||
public static double compute(final Evaluator F, double a, double b,
|
||||
final double eps) throws ConstructionException {
|
||||
double ay = F.evaluateF(a), by = F.evaluateF(b);
|
||||
double c = (a + b) / 2;
|
||||
if (ay * by > eps)
|
||||
throw new ConstructionException("");
|
||||
while (Math.abs(b - a) > eps) {
|
||||
final double cy = F.evaluateF(c);
|
||||
if (Math.abs(cy) < eps)
|
||||
return c;
|
||||
if (cy * ay > 0) {
|
||||
if (Math.abs(ay - cy) < eps) {
|
||||
a = c;
|
||||
ay = cy;
|
||||
c = (a + b) / 2;
|
||||
} else {
|
||||
final double d = a - ay * (a - c) / (ay - cy);
|
||||
if (d > b || d < a) {
|
||||
a = c;
|
||||
ay = cy;
|
||||
c = (a + b) / 2;
|
||||
} else {
|
||||
a = c;
|
||||
ay = cy;
|
||||
c = d;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final double d = a - ay * (a - c) / (ay - cy);
|
||||
b = c;
|
||||
by = cy;
|
||||
c = d;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
32
rene/zirkel/expression/Translator.java
Normal file
32
rene/zirkel/expression/Translator.java
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
|
||||
Copyright 2006 Rene Grothmann, modified by Eric Hakenholz
|
||||
|
||||
This file is part of C.a.R. software.
|
||||
|
||||
C.a.R. is a free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, version 3 of the License.
|
||||
|
||||
C.a.R. is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package rene.zirkel.expression;
|
||||
|
||||
import rene.zirkel.construction.Construction;
|
||||
|
||||
/**
|
||||
* @author Rene
|
||||
*
|
||||
*/
|
||||
public interface Translator {
|
||||
public void laterTranslate(Construction from);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue