package int_test;

import java.util.Vector;
import org.lsmp.djep.djep.DJep;
import org.lsmp.djep.djep.diffRules.MacroDiffRules;
import org.nfunk.jep.JEP;
import org.nfunk.jep.Node;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.function.Exp;

public class statyczne_metody {
/*
 * Zamiana vektora w string
 */
static String serializer(Vector<String> tab) {

int ile=tab.size();
if(ile>0) {
    String tekst="";
    for(int x=0;x<ile;x++) {
            tekst+=tab.elementAt(x);
        }
    return tekst;
    }
return "";
}
/*
 * potegowanie
 */
static double potega(double Podstawa, double Wykladnik)  {
   return Math.exp(Math.log(Podstawa) * Wykladnik);
   }
static double zaokraglenie(double var, int ile) {
     double wynik = 0.0D;
     if ((ile > 0) && (ile < 8)) {
       double potega = potega(10.0D, ile);
       wynik = Math.floor(potega * var) / potega;
       return wynik;
     }
     return 0.0D;
  }
/*
 *  Liczenie pochodnej
 */
static String pochodna(String str) {
     DJep j = new DJep();
     j.addStandardConstants();
     j.addStandardFunctions();
     j.addComplex();
     j.setAllowUndeclared(true);
     j.setAllowAssignment(true);
     j.setImplicitMul(true);
     try
     {
       j.addDiffRule(new MacroDiffRules(j, "sin", "cos(x)"));
       j.addDiffRule(new MacroDiffRules(j, "cos", "-sin(x)"));
       j.addDiffRule(new MacroDiffRules(j, "tan", "1/((cos(x))^2)"));
       j.addDiffRule(new MacroDiffRules(j, "sqrt", "1/(2 (sqrt(x)))"));
       j.addFunction("exp", new Exp());
       j.addDiffRule(new MacroDiffRules(j, "exp", "exp(x)"));

       Node node = j.parse(str);
       Node diff = j.differentiate(node, "x");

       Node simp = j.simplify(diff);

       return j.toString(simp);
     }
     catch (ParseException e) {
     }
     return "error";
   }
/*
 * Iteracja lic--
 */
static int dec(int lic) {
     if (lic > 0) {
      lic--;
       return lic;
    }return 0;
  }
/*
 * liczenie wartości równania
 */
static double licz_rownanie(String rownanie, double x) {
     JEP parser = null;
     parser = new JEP();
     parser.addStandardFunctions();
     parser.addStandardConstants();
     parser.addVariable("x", x);
     parser.parseExpression(rownanie);
     double exit = parser.getValue();
     
     if (!parser.hasError()) return exit;
    throw new IllegalStateException();
  }
/*
 * liczenia długości wektora od x1 do x2
 */
static double wektor(double x1,double x2) {
     JEP parser = null;
     parser = new JEP();
     parser.addStandardFunctions();
     parser.addStandardConstants();
     parser.setAllowUndeclared(true);
     parser.setImplicitMul(true);
     parser.addComplex();
     parser.setAllowAssignment(true);

     parser.addVariable("x1", x1);
     parser.addVariable("x2", x2);

     parser.parseExpression("c=x2-x1;");
     parser.getValueAsObject();
     parser.parseExpression("sqrt(c^2)");
     return parser.getValue();
   }
/*
 * przygotowywanie nagłówka dokumentu html
 */
static String mkStyle() {
    String style;
    style="";
    style+="<style>\n";
    style+="table {width:436px!important;float:left;}\n";
    style+="* {font-family:Arial; font-size:12px;}\n";
    style+="table.metoda { border-bottom:1px; border-collapse: collapse; float:left; width:100%; }\n";
    style+="table.metoda td { padding:3px; float:left; text-align:center; }\n";
    style+="table.metoda td.tyt { background-color:#ffc7c7; height:35px; border-width:1px; border-style:solid; border-color:red; padding-top:3px;  text-align:center; font-weight:bold; color:red; margin-bottom:5px; margin-top:8px; }\n";
    style+="table.metoda td.tyt2 { background-color:#e9b494; height:35px; border-width:1px; border-style:solid; border-color:#843b0f; padding-top:3px;  text-align:center; font-weight:bold; color:#843b0f; margin-bottom:5px; margin-top:8px; }\n";
    style+="table.metoda td.tyt3 { background-color:#e7bdff; height:35px; border-width:1px; border-style:solid; border-color:#671994; padding-top:3px;  text-align:center; font-weight:bold; color:#671994; margin-bottom:5px; margin-top:8px; }\n";
    style+="table.metoda td.rownanie { font-size:12px; color:blue; font-weight:bold; float:left; }\n";
    style+=".wynik { padding:5px; color:red; border-width:1px; border-style:solid; border-color:red; }\n";
    style+=".wynik2 { padding:5px; margin-top:5px; color:red; border-width:1px; border-style:solid; border-color:red; }\n";
    style+=".lista { width:220px; margin-top:10px; margin-bottom:10px; width:100%;}\n";
    style+=".lewy { border:1px solid #ABABAB; text-align:right; margin-right:3px;}\n";
    style+=".prawy { border:1px solid #ABABAB; text-align:left;}\n";
    style+=".lista td { padding:0px; font-size:11px; margin-bottom:2px; padding-right:2px; }\n";
    style+="tr.gray { background-color:#DEDEDE; }\n";
    style+=".wyniki { padding:5px; margin-top:5px; color:blue; border-width:1px; border-style:solid; border-color:blue; padding:3px; }\n";
    style+="</style>\n";
    return style;
    }
 }
