Ejemplo n.º 1
0
  private void addNewGeoToConstruction() {

    if (objectType == TYPE_LISTOFPOINTS || objectType == TYPE_POLYLINE) {
      app.getKernel().getConstruction().addToConstructionList(newGeo.getParentAlgorithm(), true);
    }

    newGeo.setEuclidianVisible(true);
    if (!newGeo.isGeoText()) newGeo.setAuxiliaryObject(false);

    if (objectType == TYPE_LISTOFPOINTS) {
      GeoList gl = (GeoList) newGeo;
      for (int i = 0; i < gl.size(); i++) {
        gl.get(i).setEuclidianVisible(true);
        gl.get(i).setAuxiliaryObject(false);
      }
    }

    if (objectType == TYPE_POLYLINE) {
      GeoPoint[] pts = ((AlgoPolyLine) newGeo.getParentAlgorithm()).getPoints();
      for (int i = 0; i < pts.length; i++) {
        pts[i].setEuclidianVisible(true);
        pts[i].setAuxiliaryObject(false);
      }
    }

    newGeo.update();
    app.storeUndoInfo();
  }
Ejemplo n.º 2
0
  @Override
  public final void compute() {

    double n = Math.round(num.getDouble());

    if (n == 1) {
      outputList.clear();
      outputList.setDefined(true);
      return;
    }

    if (n < 2 || n > LARGEST_INTEGER) {
      outputList.setUndefined();
      return;
    }

    outputList.setDefined(true);
    outputList.clear();

    int count = 0;

    for (int i = 2; i <= n / i; i++) {
      int exp = 0;
      while (n % i == 0) {
        exp++;
        n /= i;
      }
      if (exp > 0) {
        setListElement(count++, i, exp);
      }
    }
    if (n > 1) {
      setListElement(count++, n, 1);
    }
  }
 @Override
 protected void setInputOutput() {
   input = P.getGeoElements();
   output = new GeoElement[1];
   output[0] = implicitPoly;
   setDependencies();
 }
Ejemplo n.º 4
0
  /**
   * Creates new factorization algo
   *
   * @param cons
   * @param label
   * @param num Number to factorize
   */
  public AlgoPrimeFactorization(Construction cons, String label, NumberValue num) {
    super(cons);
    this.num = num;

    outputList = new GeoList(cons);

    setInputOutput();
    compute();
    outputList.setLabel(label);
  }
Ejemplo n.º 5
0
  @Override
  public final void compute() {

    size = inputList.size();
    if (!inputList.isDefined() || !function.toGeoElement().isDefined()) {
      r2.setUndefined();
      return;
    }

    GeoFunction funGeo = function.getGeoFunction();

    // Calculate errorsum and ssy:
    double sumyy = 0.0d;
    double sumy = 0.0d;
    double syy = 0.0d;
    double errorsum = 0.0d;
    GeoElement geo = null;
    GeoPoint point = null;
    double x, y, v;

    for (int i = 0; i < size; i++) {
      geo = inputList.get(i);
      if (geo.isGeoPoint()) {
        point = (GeoPoint) geo;
        x = point.getX();
        y = point.getY();
        v = funGeo.evaluate(x);
        errorsum += (v - y) * (v - y);
        sumy += y;
        sumyy += y * y;
      } else {
        r2.setUndefined();
        return;
      } // if calculation is possible
    } // for all points

    syy = sumyy - sumy * sumy / size;

    // calculate RSquare
    r2.setValue(1 - errorsum / syy);
  } // compute()
Ejemplo n.º 6
0
  /**
   * Resolves arguments, creates local variables and fills the vars and overlists
   *
   * @param c
   * @return list of arguments
   */
  protected final GeoElement[] resArgsForZip(Command c) {
    // check if there is a local variable in arguments
    int numArgs = c.getArgumentNumber();
    vars = new GeoElement[numArgs / 2];
    over = new GeoList[numArgs / 2];
    Construction cmdCons = (Construction) c.getKernel().getConstruction();

    for (int varPos = 1; varPos < numArgs; varPos += 2) {
      String localVarName = c.getVariableName(varPos);
      if (localVarName == null) {
        throw argErr(app, c.getName(), c.getArgument(varPos));
      }

      // add local variable name to construction

      GeoElement num = null;

      // initialize first value of local numeric variable from initPos

      boolean oldval = cons.isSuppressLabelsActive();
      cons.setSuppressLabelCreation(true);
      GeoList gl = (GeoList) resArg(c.getArgument(varPos + 1))[0];
      cons.setSuppressLabelCreation(oldval);
      num = gl.get(0).copyInternal(cons);

      cmdCons.addLocalVariable(localVarName, num);
      // set local variable as our varPos argument
      c.setArgument(varPos, new ExpressionNode(c.getKernel(), num));
      vars[varPos / 2] = num.toGeoElement();
      over[varPos / 2] = gl;
      // resolve all command arguments including the local variable just
      // created

      // remove local variable name from kernel again

    }
    GeoElement[] arg = resArgs(c);
    for (GeoElement localVar : vars) cmdCons.removeLocalVariable(localVar.getLabel());
    return arg;
  }
Ejemplo n.º 7
0
  // copied from AlgoInterationList.java
  // TODO should it be centralised?
  private void setListElement(int index, double value, double exp) {
    GeoList listElement;
    if (index < outputList.getCacheSize()) {
      // use existing list element
      listElement = (GeoList) outputList.getCached(index);
      listElement.clear();
    } else {
      // create a new list element
      listElement = new GeoList(cons);
      listElement.setParentAlgorithm(this);
      listElement.setConstructionDefaults();
      listElement.setUseVisualDefaults(false);
    }

    outputList.add(listElement);
    GeoNumeric prime = new GeoNumeric(cons);
    prime.setValue(value);
    GeoNumeric exponent = new GeoNumeric(cons);
    exponent.setValue(exp);
    listElement.add(prime);
    listElement.add(exponent);
  }
Ejemplo n.º 8
0
 @Override
 public void compute() {
   if (!factorList.isDefined() || !Kernel.isInteger(number.getDouble())) {
     result.setUndefined();
     return;
   }
   long res = 1;
   for (int i = 0; i < factorList.size(); i++) {
     GeoList pair = (GeoList) factorList.get(i);
     double exp = ((NumberValue) pair.get(1)).getDouble();
     if (sum) {
       double prime = ((NumberValue) pair.get(0)).getDouble();
       App.debug(prime);
       res = res * Math.round((Math.pow(prime, exp + 1) - 1) / (prime - 1.0));
     } else {
       res = res * Math.round(exp + 1);
     }
   }
   result.setValue(res);
 }
Ejemplo n.º 9
0
 /**
  * Creates new factorization algo
  *
  * @param cons construction
  * @param label label for output
  * @param num Number to factorize
  */
 public AlgoPrimeFactorization(Construction cons, String label, NumberValue num) {
   this(cons, num);
   outputList.setLabel(label);
 }
Ejemplo n.º 10
0
  public static GeoElement doCopyNoStoringUndoInfo0(
      Kernel kernel, App app, GeoElement value, GeoElement oldValue, int dx, int dy)
      throws Exception {
    if (value == null) {
      if (oldValue != null) {
        MatchResult matcher =
            GeoElementSpreadsheet.spreadsheetPatternPart.exec(
                oldValue.getLabel(StringTemplate.defaultTemplate));
        int column = GeoElementSpreadsheet.getSpreadsheetColumn(matcher);
        int row = GeoElementSpreadsheet.getSpreadsheetRow(matcher);

        prepareAddingValueToTableNoStoringUndoInfo(kernel, app, null, oldValue, column, row);
      }
      return null;
    }
    String text = null;

    // make sure a/0.001 doesn't become a/0

    StringTemplate highPrecision = StringTemplate.maxPrecision;
    if (value.isPointOnPath() || value.isPointInRegion()) {
      text = value.getCommandDescription(highPrecision);
    } else if (value.isChangeable()) {
      text = value.toValueString(highPrecision);
    } else {
      text = value.getCommandDescription(highPrecision);
    }

    // handle GeoText source value
    if (value.isGeoText() && !((GeoText) value).isTextCommand()) {
      // enclose text in quotes if we are copying an independent GeoText,
      // e.g. "2+3"
      if (value.isIndependent()) {
        text = "\"" + text + "\"";
      } else {

        // check if 'text' parses to a GeoText
        GeoText testGeoText = kernel.getAlgebraProcessor().evaluateToText(text, false, false);

        // if it doesn't then force it to by adding +"" on the end
        if (testGeoText == null) {
          text = text + "+\"\"";
        }
      }
    }

    // for E1 = Polynomial[D1] we need value.getCommandDescription();
    // even though it's a GeoFunction
    if (value.isGeoFunction() && text.equals("")) {
      // we need the definition without A1(x)= on the front
      text = ((GeoFunction) value).toSymbolicString(highPrecision);
    }

    boolean freeImage = false;

    if (value.isGeoImage()) {
      GeoImage image = (GeoImage) value;
      if (image.getParentAlgorithm() == null) {
        freeImage = true;
      }
    }

    // Application.debug("before:"+text);
    text = updateCellReferences(value, text, dx, dy);
    // Application.debug("after:"+text);

    // condition to show object
    GeoBoolean bool = value.getShowObjectCondition();
    String boolText = null, oldBoolText = null;
    if (bool != null) {
      if (bool.isChangeable()) {
        oldBoolText = bool.toValueString(highPrecision);
      } else {
        oldBoolText = bool.getCommandDescription(highPrecision);
      }
    }

    if (oldBoolText != null) {
      boolText = updateCellReferences(bool, oldBoolText, dx, dy);
    }

    String startPoints[] = null;
    if (value instanceof Locateable) {
      Locateable loc = (Locateable) value;

      GeoPointND[] pts = loc.getStartPoints();

      startPoints = new String[pts.length];

      for (int i = 0; i < pts.length; i++) {
        startPoints[i] = ((GeoElement) pts[i]).getLabel(highPrecision);
        startPoints[i] = updateCellReferences((GeoElement) pts[i], startPoints[i], dx, dy);
      }
    }

    // dynamic color function
    GeoList dynamicColorList = value.getColorFunction();
    String colorText = null, oldColorText = null;
    if (dynamicColorList != null) {
      if (dynamicColorList.isChangeable()) {
        oldColorText = dynamicColorList.toValueString(highPrecision);
      } else {
        oldColorText = dynamicColorList.getCommandDescription(highPrecision);
      }
    }

    if (oldColorText != null) {
      colorText = updateCellReferences(dynamicColorList, oldColorText, dx, dy);
    }

    // allow pasting blank strings
    if (text.equals("")) {
      text = "\"\"";
    }

    // make sure that non-GeoText elements are copied when the
    // equalsRequired option is set
    if (!value.isGeoText() && app.getSettings().getSpreadsheet().equalsRequired()) {
      text = "=" + text;
    }

    // Application.debug("add text = " + text + ", name = " + (char)('A' +
    // column + dx) + (row + dy + 1));

    // create the new cell geo
    MatchResult matcher =
        GeoElementSpreadsheet.spreadsheetPatternPart.exec(
            value.getLabel(StringTemplate.defaultTemplate));
    int column0 = GeoElementSpreadsheet.getSpreadsheetColumn(matcher);
    int row0 = GeoElementSpreadsheet.getSpreadsheetRow(matcher);
    GeoElement value2;
    if (freeImage || value.isGeoButton()) {
      value2 = value.copy();
      if (oldValue != null) {
        oldValue.remove();
      }
      // value2.setLabel(table.getModel().getColumnName(column0 + dx)
      //		+ (row0 + dy + 1));
      value2.setLabel(GeoElementSpreadsheet.getSpreadsheetCellName(column0 + dx, row0 + dy + 1));
      value2.updateRepaint();
    } else {
      value2 =
          prepareAddingValueToTableNoStoringUndoInfo(
              kernel, app, text, oldValue, column0 + dx, row0 + dy);
    }
    value2.setAllVisualProperties(value, false);

    value2.setAuxiliaryObject(true);

    // attempt to set updated condition to show object (if it's changed)
    if ((boolText != null)) {
      // removed as doesn't work for eg "random()<0.5" #388
      // && !boolText.equals(oldBoolText)) {
      try {
        // Application.debug("new condition to show object: "+boolText);
        GeoBoolean newConditionToShowObject =
            kernel.getAlgebraProcessor().evaluateToBoolean(boolText);
        value2.setShowObjectCondition(newConditionToShowObject);
        value2.update(); // needed to hide/show object as appropriate
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }

    // attempt to set updated dynamic color function (if it's changed)
    if ((colorText != null)) {
      // removed as doesn't work for eg "random()" #388
      // && !colorText.equals(oldColorText)) {
      try {
        // Application.debug("new color function: "+colorText);
        GeoList newColorFunction = kernel.getAlgebraProcessor().evaluateToList(colorText);
        value2.setColorFunction(newColorFunction);
        // value2.update();
      } catch (Exception e) {
        e.printStackTrace();
        return null;
      }
    }

    if (startPoints != null) {
      for (int i = 0; i < startPoints.length; i++) {
        ((Locateable) value2)
            .setStartPoint(
                kernel.getAlgebraProcessor().evaluateToPoint(startPoints[i], false, true), i);
      }

      value2.update();
    }

    // Application.debug((row + dy) + "," + column);
    // Application.debug("isGeoFunction()=" + value2.isGeoFunction());
    // Application.debug("row0 ="+row0+" dy="+dy+" column0= "+column0+" dx="+dx);

    return value2;
  }
Ejemplo n.º 11
0
  private void createNewGeo() {

    boolean nullGeo = newGeo == null;

    if (!nullGeo) {
      if (objectType == TYPE_LISTOFPOINTS) {
        GeoList gl = (GeoList) newGeo;
        for (int i = 0; i < gl.size(); i++) gl.get(i).remove();
      }

      if (objectType == TYPE_POLYLINE) {
        GeoPoint[] pts = ((AlgoPolyLine) newGeo.getParentAlgorithm()).getPoints();
        for (int i = 0; i < pts.length; i++) pts[i].remove();
      }
      newGeo.remove();
    }

    int column1 = table.selectedCellRanges.get(0).getMinColumn();
    int column2 = table.selectedCellRanges.get(0).getMaxColumn();
    int row1 = table.selectedCellRanges.get(0).getMinRow();
    int row2 = table.selectedCellRanges.get(0).getMaxRow();

    boolean copyByValue = btnValue.isSelected();
    boolean scanByColumn = cbScanOrder.getSelectedIndex() == 1;
    boolean leftToRight = cbLeftRightOrder.getSelectedIndex() == 0;
    boolean transpose = ckTranspose.isSelected();
    boolean doCreateFreePoints = true;
    boolean doStoreUndo = true;
    boolean isSorted = false;

    try {
      switch (objectType) {
        case TYPE_LIST:
          newGeo = cp.createList(selectedCellRanges, scanByColumn, copyByValue);
          break;

        case TYPE_LISTOFPOINTS:
          newGeo =
              cp.createPointGeoList(
                  selectedCellRanges,
                  copyByValue,
                  leftToRight,
                  isSorted,
                  doStoreUndo,
                  doCreateFreePoints);
          newGeo.setLabel(null);
          for (int i = 0; i < ((GeoList) newGeo).size(); i++) {
            ((GeoList) newGeo).get(i).setAuxiliaryObject(true);
            ((GeoList) newGeo).get(i).setEuclidianVisible(false);
          }
          newGeo.updateRepaint();
          break;

        case TYPE_MATRIX:
          newGeo = cp.createMatrix(column1, column2, row1, row2, copyByValue, transpose);
          break;

        case TYPE_TABLETEXT:
          newGeo = cp.createTableText(column1, column2, row1, row2, copyByValue, transpose);
          break;

        case TYPE_POLYLINE:
          newGeo = cp.createPolyLine(selectedCellRanges, copyByValue, leftToRight);
          newGeo.setLabel(null);
          GeoPoint[] pts = ((AlgoPolyLine) newGeo.getParentAlgorithm()).getPoints();
          for (int i = 0; i < pts.length; i++) {
            pts[i].setAuxiliaryObject(true);
            pts[i].setEuclidianVisible(false);
          }
          newGeo.updateRepaint();
          break;
      }

      ImageIcon latexIcon = new ImageIcon();
      // String latexStr = newGeo.getLaTeXAlgebraDescription(true);

      String latexStr = newGeo.getFormulaString(StringTemplate.latexTemplate, true);

      // System.out.println(latexStr);

      Font latexFont =
          new Font(
              app.getPlainFont().getName(),
              app.getPlainFont().getStyle(),
              app.getPlainFont().getSize() - 1);

      if (latexStr != null && newGeo.isLaTeXDrawableGeo()) {
        app.getDrawEquation()
            .drawLatexImageIcon(app, latexIcon, latexStr, latexFont, false, Color.black, null);
        lblPreview.setText(" ");
      } else {
        lblPreview.setText(newGeo.getAlgebraDescriptionTextOrHTMLDefault());
      }
      lblPreview.setIcon(latexIcon);

      if (!nullGeo) {
        newGeo.setLabel(fldName.getText());
        newGeo.setAuxiliaryObject(true);
        newGeo.setEuclidianVisible(false);
      }

      updateGUI();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 12
0
 /**
  * Clear a GeoList (i.e. remove all its items)
  *
  * @param list the GeoList to clear
  */
 public static void clearList(GeoList list) {
   list.clear();
 }
Ejemplo n.º 13
0
 /**
  * Return the length of a GeoList
  *
  * @param list the GeoList
  * @return the length of the list
  */
 public static int getListLength(GeoList list) {
   return list.size();
 }
Ejemplo n.º 14
0
 /**
  * Add an element to the end of a GeoList
  *
  * @param list the GeoList
  * @param obj the element to add
  */
 public static void appendToList(GeoList list, GeoElement obj) {
   list.add(obj);
 }
Ejemplo n.º 15
0
 /**
  * Remove an item at a given position in a GeoList
  *
  * @param list the GeoList
  * @param index the position of the item to remove
  */
 public static void removeListItem(GeoList list, int index) {
   list.remove(index);
 }
Ejemplo n.º 16
0
 /**
  * Return the item at a given position in a GeoList
  *
  * @param list the GeoList
  * @param index the index of the item
  * @return the item at position index
  */
 public static GeoElement getListItem(GeoList list, int index) {
   return list.get(index);
 }