Ejemplo n.º 1
0
        public static FunctionEntry ML_FunctionListSecondary(int request, String funcStr)
        {
            if (request == MLGUI.GetFunctionFromExpression)
            {
                FunctionTree newFunc    = new FunctionTree(funcStr);
                String       newFuncStr = newFunc.ToString();

                // Crude Function Comparison
                foreach (FunctionEntry fe in MLGUI.ML_Functions)
                {
                    if (fe.Function.ToString() == newFuncStr)
                    {
                        return(fe);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
 public override string ToString()
 {
     return(String.Format(EntryFormat, Name, Function.ToString()));
 }
Ejemplo n.º 3
0
        public static FunctionTree ML_FunctionList(int request, String name, String funcStr)
        {
            if (request == MLGUI.UpdateFunctionExpression)
            {
                foreach (FunctionEntry fe in MLGUI.ML_Functions)
                {
                    if (fe.Name == name)
                    {
                        fe.Function = new FunctionTree(funcStr);
                        if (fe.Function.HasNoVariables())
                        {
                            ML_VariableList(MLGUI.AddItem, name, fe.Function.GetValue());
                            return(null);
                        }
                        else
                        {
                            return(fe.Function);
                        }
                    }
                }
            }
            else if (request == MLGUI.GetFunction)
            {
                foreach (FunctionEntry fe in MLGUI.ML_Functions)
                {
                    if (fe.Name == name)
                    {
                        return(fe.Function);
                    }
                }
            }
            else if (request == MLGUI.AddItem)
            {
                FunctionTree newFunc    = new FunctionTree(funcStr);
                String       newFuncStr = newFunc.ToString();

                //r3.Text += "vars ";
                //foreach (FVar v in newFunc.varList) r3.Text+=v.varName + " ";

                // Crude Function Comparison
                foreach (FunctionEntry fe in MLGUI.ML_Functions)
                {
                    if (fe.Function.ToString() == newFuncStr)
                    {
                        if (name != "")
                        {
                            fe.Name = name;
                        }
                        return(fe.Function);
                    }
                }

                if (name == "")
                {
                    int  x         = 1;
                    bool nameTaken = true;
                    while (nameTaken)
                    {
                        name      = "f" + x;
                        nameTaken = false;

                        foreach (FunctionEntry fe in MLGUI.ML_Functions)
                        {
                            if (fe.Name == name)
                            {
                                nameTaken = true;
                                break;
                            }
                            else
                            {
                                nameTaken = false;
                            }
                        }

                        if (nameTaken)
                        {
                            x++;
                        }
                        else
                        {
                            break;
                        }
                    }

                    MLGUI.lastFunctionEntry = new FunctionEntry(name, newFunc);
                    MLGUI.ML_Functions.Add(MLGUI.lastFunctionEntry);
                    return(newFunc);
                }
                else
                {
                    foreach (FunctionEntry fe in MLGUI.ML_Functions)
                    {
                        if (fe.Name == name)
                        {
                            fe.Function = new FunctionTree(funcStr);
                            return(fe.Function);
                        }
                    }

                    ML_VariableList(MLGUI.RemoveItem, name);

                    MLGUI.lastFunctionEntry = new FunctionEntry(name, newFunc);
                    MLGUI.ML_Functions.Add(MLGUI.lastFunctionEntry);

                    if (MLGUI.lastFunctionEntry.Function.HasNoVariables())
                    {
                        ML_VariableList(MLGUI.AddItem, name, MLGUI.lastFunctionEntry.Function.GetValue());
                    }
                    else
                    {
                        return(newFunc);
                    }
                }
            }
            else if (request == MLGUI.RemoveItem)
            {
                foreach (FunctionEntry fe in MLGUI.ML_Functions)
                {
                    if (fe.Name == name)
                    {
                        FunctionTree temp = fe.Function;
                        MLGUI.ML_Functions.Remove(fe);
                        return(temp);
                    }
                }
            }
            else if (request == MLGUI.RemoveLastItem)
            {
                MLGUI.ML_Functions.Remove(MLGUI.lastFunctionEntry);
                return(MLGUI.lastFunctionEntry.Function);
            }
            return(null);
        }
Ejemplo n.º 4
0
        public static void ParseCommand()
        {
            // commands = { "solve", "value", "derivative", "integral", "roots", "plot" , "simplify"};

            switch (comm_inx)
            {
            case 0:         // solve
            case 1:         // value
            {
            }
            break;

            case 2:         // derivative
            {
                int bracket = 0;

                // Parse Function
                while (NotEnd())
                {
                    if (bracket == 0 && p_line[inx] == ',')
                    {
                        break;
                    }
                    if (p_line[inx] == '(')
                    {
                        bracket++;
                    }
                    if (p_line[inx] == ')')
                    {
                        bracket--;
                    }
                    synline[syn_inx] += p_line[inx];
                    inx++;
                }

                if (synline[syn_inx] == "")
                {
                    syntaxError = true;
                    Error();
                    break;
                }

                bool isVariable     = false;
                bool isFunction     = false;
                bool isFunctionName = false;

                int func_fn_inx = 0;
                int var_fn_inx  = 0;

                // Try Parsing Function or Variable
                // All Variables get Added to ML_Variables
                if (isFunctionName = IsExistingFunctionName(synline[syn_inx]))
                {
                    funcs[func_inx] = is_existingFunc;
                    func_inx++;

                    if (End())
                    {
                        output[output_inx] = funcs[func_fn_inx].GetDerivative(funcs[func_fn_inx].VarList[0]).ToString();
                        break;
                    }
                }
                else if (isVariable = IsVariable(synline[syn_inx]))
                {
                    vars[var_inx] = ListHandler.ML_VariableList(MLGUI.AddItem, is_var);
                    var_fn_inx    = var_inx;
                    var_inx++;

                    if (End())
                    {
                        FunctionTree temp = new FunctionTree(vars[var_fn_inx]);
                        output[output_inx] = temp.GetDerivative(vars[var_fn_inx]).ToString();
                        break;
                    }
                }
                else if (isFunction = IsFunction(synline[syn_inx]))
                {
                    funcs[func_inx] = ListHandler.ML_FunctionList(MLGUI.AddItem, "", is_func);
                    func_fn_inx     = func_inx;
                    func_inx++;

                    if (End())
                    {
                        output[output_inx] = funcs[func_fn_inx].GetDerivative(funcs[func_fn_inx].VarList[0]).ToString();
                        break;
                    }
                }
                else
                {
                    syntaxError = true;
                    Error();
                    break;
                }

                // Parse Further
                SkipSpaces();
                inx--;
                if (End())
                {
                    break;
                }

                bool   wrtSet       = false;
                FVar   wrtVar       = null;
                double wrtVarVal    = 0;
                bool   wrtVarValSet = false;
                syn_inx++;
                List <string> varEqList = new List <string>();

                // Next vars[var_inx] Entry will be the w.r.t. variable
                int func_sl_inx = syn_inx;

                // Parse rest of the line
                // Requires addition of "," at end
                p_line = p_line + ",";
                p_len  = p_line.Length;

                inx++;

                while (NotEnd())
                {
                    if (p_line[inx] == ',')
                    {
                        if (!isVariable && IsEquation(synline[syn_inx]) && SplitEquation(synline[syn_inx], VariableAssignment))
                        {
                            if (ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, is_var, is_val) == null)
                            {
                                syntaxError = true;
                                break;
                            }
                            else
                            {
                                varEqList.Add(is_var);
                            }
                        }
                        else if (IsNumber(synline[syn_inx]) && !wrtVarValSet)
                        {
                            wrtVarVal = is_val;
                            if (wrtSet)
                            {
                                ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, wrtVar.varName, is_val);
                                wrtVarVal = is_val;
                            }
                            wrtVarValSet = true;
                        }
                        else if (IsVariable(synline[syn_inx]) && !wrtSet)
                        {
                            wrtVar = ListHandler.ML_VariableList(MLGUI.AddItem, is_var);
                            wrtSet = true;
                        }
                        else
                        {
                            syntaxError = true;
                            break;
                        }
                        syn_inx++;
                    }
                    else
                    {
                        synline[syn_inx] += p_line[inx];
                    }

                    inx++;
                }

                if (Error())
                {
                    break;
                }

                if (isVariable)
                {
                    if (!wrtSet)
                    {
                        wrtVar = vars[var_fn_inx];
                        if (wrtVarValSet)
                        {
                            ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, wrtVar.varName, wrtVarVal);
                        }
                    }

                    output[output_inx] += vars[var_fn_inx].GetDerivative(wrtVar).ToString();
                    break;
                }

                if (!wrtSet)
                {
                    wrtVar = funcs[func_fn_inx].varList[0];
                }

                if (wrtVarValSet)
                {
                    ListHandler.ML_VariableList(MLGUI.UpdateVariableValue, wrtVar.varName, wrtVarVal);
                }

                FunctionTree copy = funcs[func_fn_inx].GetCopy();

                foreach (string varEq in varEqList)
                {
                    if (varEq == wrtVar.varName)
                    {
                        continue;
                    }
                    if (!copy.VariableToSetValue(varEq))
                    {
                        syntaxError = true;
                        break;
                    }
                }

                FunctionTree der = copy.GetDerivative(wrtVar);

                if (Error())
                {
                    break;
                }

                // Store result in output string
                if (der.varList.Count == 0)
                {
                    output[output_inx] = der.GetValue().ToString();

                    // Important to figure out properly
                    comm_isAnsNumVal = true;

                    // Single answer
                    ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                    ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                    ListHandler.ML_VariableList(MLGUI.AddItem, "ans", der.GetValue());
                }
                else
                {
                    output[output_inx] = der.ToString();

                    if (der.CanHaveValue())
                    {
                        string derVal = der.GetValue().ToString();

                        if (!(derVal == output[output_inx]))
                        {
                            output_inx++;
                            output[output_inx] = derVal;

                            // Important to figure out properly
                            comm_isAnsFunc   = true;
                            comm_isAnsNumVal = true;

                            // Two Answers
                            ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans0");
                            ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans0");
                            ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                            ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                            ListHandler.ML_FunctionList(MLGUI.AddItem, "ans0", der.ToString());
                            ListHandler.ML_VariableList(MLGUI.AddItem, "ans", der.GetValue());
                        }
                        else
                        {
                            // Important to figure out properly
                            comm_isAnsNumVal = true;
                            ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                            ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                            ListHandler.ML_VariableList(MLGUI.AddItem, "ans", der.GetValue());
                        }
                    }
                    else
                    {
                        // Important to figure out properly
                        comm_isAnsFunc = true;

                        // Single answer
                        ListHandler.ML_FunctionList(MLGUI.RemoveItem, "ans");
                        ListHandler.ML_VariableList(MLGUI.RemoveItem, "ans");

                        ListHandler.ML_FunctionList(MLGUI.AddItem, "ans", der.ToString());
                    }
                }
            }
            break;

            case 3:         // integral
            {
            }
            break;

            case 4:         // roots
            {
            }
            break;

            case 5:         // plot
            {
            }
            break;

            case 6:         // simplify
            {
            }
            break;

            case 7:         // cls
            {
                r2.SelectAll(); r2.SelectionProtected = false;
                r2.Clear();
                r3.Clear();
                mlgui.AddLineNumbers(0);
            }
            break;

            default: break;
            }
        }