Use NSPredicate to parse a formula with variables - objective-c

Task:
I am planning to parse a formula string in NSPredicate and to replace variables in the string by their numeric values. The variables are names for properties of existing object instances in my data model, for instance I have a class "company" with an instance "Apple Corp."
Set-up:
My formula would like look like this: "Profitability_2011_in% = [Profit 2011] / [Revenue 2011]"
The instance "Apple Corp" would have the following properties:
Revenue 2009 = 10, Revenue 2010 = 20, Revenue 2011 = 30,
Profit 2009 = 5, Profit 2010 = 10, Profit 2011 = 20.
Hence, the formula would yield 20 / 30 = 67%.
Variables are usually two-dimensional, for instance defined by "profit" as the financial statement item and "year" (for instance 2011).
The variables are enclosed in [ ] and the dimensions are separated by " " (whitespace).
How I would do it
My implementation would begin with NSRegularExpression's matchesInString:options:range: to get an array of all variables in the formula (Profit 2011, Revenue 2011) and then construct an NSDictionary (key = variable name) out of this array by querying my data model.
What do you think?
Is there a better way to do it in your view?
In the formula, how would you replace the variables by their values?
How would you parse the formula?
Thank you!!

Yes, you can do this. This falls under the category of "Using NSPredicate for things for which it was not intended", but will work just fine.
You'll need to replace your variables with a single word that start with a $, since that's how NSPredicate denotes variables:
NSPredicate *p = [NSPredicate predicateWithFormat:#"foo = $bar"];
However you want to do that, great. NSRegularExpression is a fine way to do that.
Once you do that, you'll have something like this:
#"$profitability2011 = $profit2011 / $revenue2011"
You can then pop this through +predicateWithFormat:. You'll get back an NSComparisonPredicate. The -leftExpression will be of type NSVariableExpressionType, and the -rightExpression will be of type NSFunctionExpressionType.
This is where things start to get hairy. If you were to -evaluteWithObject:substitutionVariables:, you'd simply get back a YES or NO value, since a predicate is simply a statement that evaluates to true or false. I haven't explored how you could just evaluate one side (in this case, the -rightExpression), but it's possible that -[NSExpression expressionValueWithObject:context:] might help you. I don't know, because I'm not sure what that "context" parameter is for. It doesn't seem like it's a substitution dictionary, but I could be wrong.
So if that doesn't work (and I have no idea if it will or not), you could use my parser: DDMathParser. It has a parser, similar to NSPredicate's parser, but is specifically tuned for parsing and evaluating mathematical expressions. In your case, you'd do:
#import "DDMathParser.h"
NSString *s = #"$profit2011 / $revenue2011";
NSDictionary *values = ...; // the values of the variables
NSNumber *profitability = [s numberByEvaluatingStringWithSubstitutions:values];
The documentation for DDMathParser is quite extensive, and it can do quite a bit.
edit Dynamic variable resolution
I just pushed a change that allows DDMathParser to resolve functions dynamically. It's important to understand that a function is different from a variable. A function is evaluated, whereas a variable is simply substituted. However, the change only does dynamic resolution for functions, not variables. That's ok, because DDMathParser has this neat thing called argumentless functions.
An argumentless function is a function name that's not followed by an opening parenthesis. For convenience, it's inserted for you. This means that #"pi" is correctly parsed as #"pi()" (since the constant for π is implemented as a function).
In your case, you can do this:
Instead of regexing your string to make variables, simply use the names of the terms:
#"profit_2011 / revenue_2011";
This will be parsed as if you had entered:
#"divide(profit_2011(), revenue_2011())"
You can the set up your DDMathEvaluator object with a function resolver. There are two examples of this in the DDMathParser repository:
This example shows how to use the resolver function to look up the "missing" function in a substitution dictionary (this would be most like what you want)
This example shows you to interpret any missing function as if it evaluated to 42.
Once you implement a resolver function, you can forego having to package all your variables up into a dictionary.

Is there a better way to do it in your view?
Yes - using Flex & Bison.
Possibly you could achieve what you want with a regex - but for many expression grammars, a regex isn't powerful enough to parse the grammar. Also, regex things like this get large, unreadable, and unyieldy.
You can use Flex (a lexer) and Bison (a parser) to create a grammar definition for your expressions, and generate C code (which, as I'm sure you know, works perfectly with Objective-C since Objective-C is C) which you can use to parse your expressions.
In the formula, how would you replace the variables by their values?
As you parse through it with Bison you should have a hash table with variable names and their current values. When you generate the syntax tree, add references to the variables to your syntax tree nodes.
How would you parse the formula?
Again - Flex & Bison are specifically meant to do this kind of thing - and they excel at it.

Related

Evaluating Variables in Load Script

Is there any reason that this syntax shouldn't work in Qlikview load script??
Let v_myNumber = year(today());
Let v_myString = '2017-08';
If left($(v_myString),4) = text($(v_myNumber)) Then
'do something
Else
'do something else
End If;
I've tried both ways where I convert variable string to number and evaluate against the number variable directly and this way. They won't evaluate to equivalence when they should..
Left function is expecting a string as is getting something else as a parameter. As you are currently doing, the function will be called as Left(2017-08, 4) which is unhandle by QlikView.
If you use Left('$(v_myString)',4), it will evaluate as Left('2017-08', 4) as work as expected. Just adding quotes around the variable it should work.
Although QlikView calls them variables, they should really be seen as "stuff to replaced (at sometimes evaluated) at runtime", which is slightly different from a standard "variable" behaviour.
Dollar sign expansion is a big subject, but in short:
if you are setting a variable - no need for $().
if you are using a variable - you can use $(). depends on its context.
if you are using a variable that needs to be evaluated - you have to use $().
for example in a load script: let var1 = 'if(a=1,1,2)' - here later on the script you will probably want to use this variable as $(var1) so it will be evaluated on the fly...
I hope its a little more clear now. variable can be used in many ways at even can take parameters!
for example:
var2 = $1*$2
and then you can use like this: $(var2(2,3)) which will yield 6
For further exploration of this, I would suggest reading this

How to consistently replace atoms with variables in Prolog?

I am writing a parser for SPARQL (Semantic Web query language) using DCG. I want to replace SPARQL variable names with Prolog variables. How would I go about this?
I can generate new variables using length([NewVar], 1), but I cannot keep track of existing assignments by simply using a list of name-variable pairs. A member/2 operation on the list will return a new variable, not the one stored in the list.
Is there an easy way for naming variables in Prolog, e.g., '$VAR(Name)'?
member/2 will do what you want. Here is an example:
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.3.25)
Copyright (c) 1990-2016 University of Amsterdam, VU Amsterdam
L=[a-X,b-Y,c-Z], member(b-V,L).
L = [a-X, b-V, c-Z],
Y = V
But you might get problems if you interleave write/1 with member/2,
since a variable might change its identity, i.e. the write symbol in the following circumstances:
because of garbage collection, if a variable is written as _G<memloc>
because of aliasing, in the above example the memloc of V might be shown
instead of the memloc of Y
Same problem with (#<)/2. One way out is to use attribute variables, which at least puts an end to aliasing, since attribute variables are usually unified last,
so in the above example if Y is an attribute variable and V is an ordinary variable you would never see the memloc of V after
calling member/2.
Further you can also mitigate the problem by using ISO core standard variable_names/1 write option, to write out a variablified term. The variable_names/1 write option is immune to garbage collection or aliasing.
Bye

How to avoid using "and" in Objective-C method name

I have this method in my NDVector class which finds the angle between two vectors along two axes (for 3D vectors this means you can find the angle between them just in terms of their X and Y components, X and Z components, or Y and Z components):
- (float) angleBetweenThisAnd: (NDVector *) vector2 byAxis: (NSUInteger) axis1 and: (NSUInteger) axis2;
I don't think I'm utilizing the descriptive naming you can do with methods which take multiple parameters in Objective-C. I find myself doing this a lot, really. I think of the method, say it as a sentence which doesn't state the relationship between its subject and its verb in plain English (i.e. [someVector angleBetweenThisAnd: otherVec ...] instead of [someVector findsAngleBetweenItselfAnd: otherVec ...]) and then write it as the method name, but it seems so redundant to say "and" in the name of a method. I mean of course it's and!
In Java, naming methods was a lot simpler, but in Objective-C, I'm confused by the close relationship between plain English and code in method names. Most importantly, is there a common way to avoid using "and" in the name of a method?
Look at some of the NSDate comparison methods for inspiration.
For example, following a pattern like timeIntervalSinceDate: or descriptionWithCalendarFormat:timeZone:locale:, how about [NDVector angleWhenIntersectingWithVector:axis1:axis2]?
This could look like:
- (float) angleWhenIntersectingWithVector:(NDVector *)vector2
axis1:(NSUInteger)axis1
axis2:(NSUInteger)axis2;
It's possible to write method parameters that have no name. This is uncommon, but is occasionally the cleanest way to style the code.
- (float) angleWithVector:(NDVector *)vector2 axes:(NSUInteger)axis1 :(NSUInteger)axis2;
[vector angleWithVector:otherVector axes:b :K]
#selector(angleWithVector:axes::)
If you look at your method you made, you're really not being descriptive with the method itself. You are correct, using "and" is never good, using "and" as a parameter description is horrible. Think of it this way, if you don't have the input parameters named, how can you (or someone else) tell what it is (i.e., inline in code)?
For example:
- (float) angleBetweenThisAnd: byAxis: and:
in use:
[currentThing angleBetweenThisAnd:myVec byAxis:b and:K];
What is "this" in the first input parameter? What should I pass to "and"??? What is an "and" value supposed to be? Maybe you're the only one using your code, but time will come you haven't looked at it and forget how it works.
Think about putting a descriptive word in the method name. By having the method names with descriptive text, anyone else can read it, especially when you call it in code, since the values won't have their types or default names. I would rewrite your method as such:
-(float)angleBetweenVector2:(NDVector *)vector2 withAxis1:(NSUInteger)axis1 withAxis2:(NSUInteger)axis2;
Another approach addressing the title question is to ask
"How does the signature work when there are no argument names"
So your original version.
angleBetweenThisAnd:byAxis:and: doesn't scan so well.
Whereas Aaron's version does…
angleWhenIntersectingWithVector:axis1:axis2:
The method signature now gives a clear indication of what it does without embedding info in the arg names.
After your comment: If you have the freedom you could use an enumeration rather than a struct:
typedef enum { XAxis = 1,
YAxis = 2,
ZAxis = 4,
XYPlane = 3,
XZPlane = 5,
YZPlane = 6
} AxisOrPlane;
- (float) angleAtIntersectionWith:(NDVector *)vector inPlane:(AxisOrPlane)plane
The enumeration values have been picked so XYPlane is equivalent to XAxis | YAxis etc., but you don't need to do that.
A struct will of course work as well, for example:
typedef struct { NSUInteger axis1; NSUInteger axis2; } Plane;
- (CGFloat) angleAtIntersectionWith:(NSArray *)vector inPlane:(Plane)plane
and when calling the method you can use a structure literal:
[aVector angleAtIntersectionWith:anotherVector inPlane:(Plane){XAxis,ZAxis}];
where XAxis and YAxis are defined appropriately.
HTH

Is it possible to parse a mathematical expression by using #define?

I want to make a scientific calculator in which the user enters something like 3+4*(3-5)/23 and then the calculator can return the value.
Now I'm trying to find a way to parse a string of mathematical expression. I know that there are some built parsers and algorithms but I want to know whether it's possible by using #define method.
Basically, I want to use the #define to literally remove the # and " " in a string and make it look like an expression that can be evaluated. At this stage, I won't use unknown variables like x or 3*k or a*b/c. All will be numbers and operators like 3+4 and 32 that can be directly evaluated by the compiler. Here is what I want to write in #define:
#define eval#"(x)" x
In the above code, eval is just a signal of parsing and the #"x" is the actual string that need to parse and x is a mathematical expression. After the translation, only x will remain. For example, if I write
double result = eval#"(3+4)";
the compiler will read
double result = 3+4;
(according to my understanding of #define). However, the code does not work. I suspect that the quotation marks confuse the compiler and cause the code to break. So my question is: can anyone come up with a solution using #define?
This is not possible with the preprocessor, no string manipulation besides concatenation supported.
Why would you need the #"x" syntax anyways? You can just put the expression right there in the code.
People are right, you cannot do it in direct way, however if you very want macro:
#define eval(x) [[[NSExpression expressionWithFormat:x] expressionValueWithObject:nil context:nil] doubleValue]
double result = eval(#"3+4");
#define is an invocation of the C preprocessor, which is not capable of this kind of manipulation. It almost sounds like you're trying to define an Objective-C macro that would do the same kind of thing as a LISP macro, but that's not possible. Why don't you tell us what the original problem is that you're trying to solve... I think we can probably come up with an easier way to do what you're trying to do.

can a variable have multiple values

In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?
Is it possible to say something like i = 3 or 2;?
This is one of the features planned for Perl 6 (junctions), with syntax that should look like my $a = 1|2|3;
If ever implemented, it would work intuitively, like $a==1 being true at the same time as $a==2. Also, for example, $a+1 would give you a value of 2|3|4.
This feature is actually available in Perl5 as well through Perl6::Junction and Quantum::Superpositions modules, but without the syntax sugar (through 'functions' all and any).
At least for comparison (b < any(1,2,3)) it was also available in Microsoft Cω experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).
You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.
You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.
numpy arrays do something similar for vectors and matrices.
That's also the kind of thing you can do in Prolog. You define rules that constraint your variables and then let Prolog resolve them ...
It takes some time to get used to it, but it is wonderful for certain problems once you know how to use it ...
Damien Conways Quantum::Superpositions might do what you want,
https://metacpan.org/pod/Quantum::Superpositions
You might need your crack-pipe however.
What you're asking seems to be how to implement a Fuzzy Logic system. These have been around for some time and you can undoubtedly pick up a library for the common programming languages quite easily.
You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.
A variable is nothing more than an address into memory. That means a variable describes exactly one place in memory (length depending on the type). So as long as we have no "quantum memory" (and we dont have it, and it doesnt look like we will have it in near future), the answer is a NO.
If you want to program and to modell this behaviour, your way would be to use a an array (with length equal to the number of max. multiple values). With this comes the increased runtime, hence the computations must be done on each of the values (e.g. x+y, must compute with 2 different values x1+y1, x2+y2, x1+y2 and x2+y1).
In Perl , you can .
If you use Scalar::Util , you can have a var take 2 values . One if it's used in string context , and another if it's used in a numerical context .