File: math-function.vala

package info (click to toggle)
gnome-calculator 3.30.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,760 kB
  • sloc: python: 168; xml: 121; makefile: 4
file content (284 lines) | stat: -rw-r--r-- 8,370 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2013 Garima Joshi
 *
 * This program is 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, either version 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

public class MathFunction : Object
{
    private string _name;
    private string[] _arguments;
    private string? _expression;
    private string? _description;

    public string name {
        get { return _name; }
    }

    public string[] arguments {
        get { return _arguments; }
    }

    public string? expression {
        get { return _expression; }
    }

    public string? description {
        get { return _description; }
    }

    public MathFunction (string function_name, string[] arguments, string? expression, string? description)
    {
        _name = function_name;
        _arguments = arguments;

        if (expression != null)
            _expression = expression;
        else
            _expression = "";

        if (description != null)
            _description = description;
        else
            _description = "";
    }

    public virtual Number? evaluate (Number[] args, Parser? root_parser = null)
    {
        FunctionParser parser = new FunctionParser (this, root_parser, args);

        uint representation_base;
        ErrorCode error_code;
        string? error_token;
        uint error_start;
        uint error_end;

        var ans = parser.parse (out representation_base, out error_code, out error_token, out error_start, out error_end);
        if (error_code == ErrorCode.NONE)
            return ans;

        root_parser.set_error (error_code, error_token, error_start, error_end);
        return null;
    }

    public bool validate (Parser? root_parser = null)
    {
        if (!is_name_valid (name))
        {
            root_parser.set_error (ErrorCode.INVALID);
            return false;
        }
        foreach (var argument in arguments)
        {
            if (!is_name_valid (argument))
            {
                root_parser.set_error (ErrorCode.INVALID);
                return false;
            }
        }

        Number[] args = {};
        FunctionParser parser = new FunctionParser (this, root_parser, args);

        uint representation_base;
        ErrorCode error_code;
        string? error_token;
        uint error_start;
        uint error_end;

        parser.create_parse_tree (out representation_base, out error_code, out error_token, out error_start, out error_end);
        if (error_code == ErrorCode.NONE)
            return true;

        root_parser.set_error (error_code, error_token, error_start, error_end);
        return false;
    }

    private bool is_name_valid (string x)
    {
        for (int i = 0; i < x.length; i++)
        {
            unichar current_char = x.get_char (i);
            if (!current_char.isalpha ())
                return false;
        }
        return true;
    }

    public virtual bool is_custom_function ()
    {
        return true;
    }
}

public class ExpressionParser : Parser
{
    private Parser? _root_parser;

    public ExpressionParser (string expression, Parser? root_parser = null)
    {
        base (expression, root_parser.number_base, root_parser.wordlen, root_parser.angle_units);
        _root_parser = root_parser;
    }

    protected override bool variable_is_defined (string name)
    {
        if (base.variable_is_defined (name))
            return true;

        return _root_parser.variable_is_defined (name);
    }

    protected override Number? get_variable (string name)
    {
        var value = base.get_variable (name);
        if (value != null)
            return value;
        return _root_parser.get_variable (name);
    }

    protected override bool function_is_defined (string name)
    {
        if (base.function_is_defined (name))
            return true;
        return _root_parser.function_is_defined (name);
    }
}

private class FunctionParser : ExpressionParser
{
    private Number[] _parameters;
    private MathFunction _function;
    public FunctionParser (MathFunction function, Parser? root_parser = null, Number[] parameters)
    {
        base (function.expression, root_parser);
        _function = function;
        _parameters = parameters;
    }

    protected override bool variable_is_defined (string name)
    {
        string[] argument_names = _function.arguments;
        for (int i = 0; i < argument_names.length; i++)
        {
            if (argument_names[i] == name)
                return true;
        }
        return base.variable_is_defined (name);
    }

    protected override Number? get_variable (string name)
    {
        string[] argument_names = _function.arguments;
        for (int i = 0; i < argument_names.length; i++)
        {
            if (argument_names[i] == name)
            {
                if (_parameters.length > i)
                    return _parameters[i];
                return null;
            }
        }
        return base.get_variable (name);
    }
}

public class BuiltInMathFunction : MathFunction
{
    public BuiltInMathFunction (string function_name, string? description)
    {
        string[] arguments = {};
        string expression = "";
        base (function_name, arguments, expression, description);
    }

    public override Number? evaluate (Number[] args, Parser? root_parser = null)
    {
        return evaluate_built_in_function (name, args, root_parser);
    }

    public override bool is_custom_function ()
    {
        return false;
    }
}

private Number? evaluate_built_in_function (string name, Number[] args, Parser? root_parser = null)
{
    var lower_name = name.down ();
    var x = args[0];
    // FIXME: Re Im ?

    if (lower_name == "log")
    {
        if (args.length <= 1)
            return x.logarithm (10); // FIXME: Default to ln
        else
        {
            var log_base = args[1].to_integer ();
            if (log_base < 0)
                return null;
            else
                return x.logarithm (log_base);
        }
    }
    else if (lower_name == "ln")
        return x.ln ();
    else if (lower_name == "sqrt") // √x
        return x.sqrt ();
    else if (lower_name == "abs") // |x|
        return x.abs ();
    else if (lower_name == "sgn") //signum function
        return x.sgn ();
    else if (lower_name == "arg")
        return x.arg (root_parser.angle_units);
    else if (lower_name == "conj")
        return x.conjugate ();
    else if (lower_name == "int")
        return x.integer_component ();
    else if (lower_name == "frac")
        return x.fractional_component ();
    else if (lower_name == "floor")
        return x.floor ();
    else if (lower_name == "ceil")
        return x.ceiling ();
    else if (lower_name == "round")
        return x.round ();
    else if (lower_name == "re")
        return x.real_component ();
    else if (lower_name == "im")
        return x.imaginary_component ();
    else if (lower_name == "sin")
        return x.sin (root_parser.angle_units);
    else if (lower_name == "cos")
        return x.cos (root_parser.angle_units);
    else if (lower_name == "tan")
        return x.tan (root_parser.angle_units);
    else if (lower_name == "sin⁻¹" || lower_name == "asin")
        return x.asin (root_parser.angle_units);
    else if (lower_name == "cos⁻¹" || lower_name == "acos")
        return x.acos (root_parser.angle_units);
    else if (lower_name == "tan⁻¹" || lower_name == "atan")
        return x.atan (root_parser.angle_units);
    else if (lower_name == "sinh")
        return x.sinh ();
    else if (lower_name == "cosh")
        return x.cosh ();
    else if (lower_name == "tanh")
        return x.tanh ();
    else if (lower_name == "sinh⁻¹" || lower_name == "asinh")
        return x.asinh ();
    else if (lower_name == "cosh⁻¹" || lower_name == "acosh")
        return x.acosh ();
    else if (lower_name == "tanh⁻¹" || lower_name == "atanh")
        return x.atanh ();
    else if (lower_name == "ones")
        return x.ones_complement (root_parser.wordlen);
    else if (lower_name == "twos")
        return x.twos_complement (root_parser.wordlen);
    return null;
}