I am using DDMathParser to parse the string expressions. I am working on one expression which is "(2+x)6+2x+2y=5y+2x", I am trying to parse it and evaluate it using DDMathParser. It can be considered as first degree function that I need to solve using Objective C.
Guys any ideas?
Help would be highly appreciated.
DDExpression * e = [DDExpression expressionFromString:expression error:error];
NSNumber *num = [e evaluateWithSubstitutions:substitutions evaluator:nil error:error];
I don't know much about DDMathParser (OK, I don't know anything about it), but taking a look at the documentation gives some clues about what is going wrong with your code.
Based on the documentation, I could find not example of where DDMathParser could handle symbolic expressions. All of the examples on the Usage page show numeric expressions.
For symbolic math, see Symbolic Math Library in C/C++/Obj-C .
In addition, it looks like you are trying to solve for two variables with a single equation, which is certainly not going to result in a numeric solution.
However, if you are just trying to substitute values in for x and y, you should use the $ sign before them as the documentation says:
If you don't know what the value of a particular term should be when
the string is constructed, that's ok; simply use a variable:
NSString *math = #"6 * $a";
So if you just want to substitute values in your expression, try using:
string expression = #"(2+$x)6+2*$x+2*$y";
Then fill in with values.
NSDictionary *s = #{#"x" : 42, #"y" : 43};
And evaluate.
DDMathEvaluator *eval = [DDMathEvaluator sharedMathEvaluator];
NSLog(#"%#", [eval evaluateString:expression withSubstitutions:s]);
Related
Can anybody duplicate this result? I'm testing rangeOfMisspelledWordInString (in iOS) to find mispelled words and some random letters return a valid word as shown below.
UITextChecker* pSpellChecker = [[[UITextChecker alloc] init] autorelease];
NSRange rangeWord = NSMakeRange(0, 8);
NSRange rangeCheck = [_pSpellChecker rangeOfMisspelledWordInString:#"lhpcjeuw"
range:rangeWord
startingAt:0
wrap:NO
language:#"en_US"];
if (rangeCheck.location == NSNotFound) {
NSLog(#"Valid Word:");
}
Below are some of the words that are also valid according to rangeOfMisspelledWordInString:
BTW, I've made sure to convert the following words to lowercase before testing.
LD
THY
THE
THECA
TD
HL
HT
YD
YLEQXXH
DV
DVX
DVXX
DVXXD
DVXXDX
DVXXX
DVHXG
DVHEJWCP
DH
DH
DPJLEHHY
Very strange. Am I doing something wrong?
I think "the" and "thy" just might be valid words ;)
Other than that, my best guess is that the text system can't provide a guess for the word, and so ignores it entirely - the semantic meaning of "misspelled word" might not include "strings of letters which cannot conceivably be a misspelling of a word." I notice that when I type those strings into a system text field (e.g. in Messages), I don't get any replacement suggestions.
You could also make sure that your UITextChecker instance isn't set to ignore those particular words; take a look at the ignoredWords property.
Yes, I can reproduce it, and I would call this a bug. If I put in your test word, #"lhpcjeuw", it treats it as a valid word. But, if I use #"lahpcjeuw" (added an "a" in second position), it catches it. I noticed the same thing Tim did -- when writing this answer, the spell checker underlined that second one but not the first as I typed.
I want to create a string. Based on condition I have to append some extra string. In this case which is preferred to use? Whether creating two different strings based on condition or creating mutable string to append a new string based on condition ?
EX
if(a==1)
{
String = "apple seed"
}
else
{
String = "apple"
}
Or
NSMutableString *string ;
string = #"apple";
if( a==1)
{
[string appendString:#"seed"]
}
A string literal, like your #"apple", is a compile-time constant so assigning a string literal to a variable of type NSString * is a cheap operation.
So for your particular examples the first selects one of two simple assignments, while the second can do a simple assignment and a method call - which is clearly going to take a little more time.
That said a "little more" on a modern computer is not long. Beware of optimising prematurely; it is far better to write code that is clear and understandable to you first and concern yourself over performance of minutiae later if needed (this is not an excuse to write poor algorithms or intentionally bad code of course).
HTH
I use NSRegularExpression to find matched by a certain patter, which is visible in the snippet:
- (NSString *)functionPattern
{
return #"[A-Za-z]{1,}\\([A-Za-z0-9,\\(\\)]{1,}\\)";
}
- (void)test
{
NSString *formula = #"AVERAGE(G17,G18,AVERAGE(G20,G21,MIN(G30,G31)))";
NSError *error;
NSRegularExpression *functionRegex = [NSRegularExpression regularExpressionWithPattern:[self functionPattern]
options:NSRegularExpressionCaseInsensitive
error:&error];
if (error) {
NSLog(#"error");
return;
}
NSArray *matches = [functionRegex matchesInString:formula options:0 range:NSMakeRange(0, formula.length)];
for (NSUInteger i = 0; i < matches.count; i++) {
NSTextCheckingResult *result = matches[i];
NSString *match = [formula substringWithRange:result.range];
NSLog(#"%#", match);
}
}
My natural expectation was to get 3 matches: AVERAGE(...), AERAGE(...) and MIN(...). Surprisingly for me, i only get one match: AVERAGE(G17,G18,AVERAGE(G20,G21,MIN(G30,G31))).
If the formula is AVERAGE(G17,G18)+AVERAGE(G20,G21,MIN(G30,G31)), i'll get 2 matches: AVERAGE(G17,G18) and AVERAGE(G20,G21,MIN(G30,G31)). In other words, after a match is found, a search for the pattern is not performed in the range of the matched string.
Please advice how to overcome this, and find all possible matches. Am i missing something simple here?
What i'm doing is parsing and evaluating math expressions. All works fine, except for the cases of nested functions. If i know all possible function names in advance, can that be utilised somehow?
I'm hoping to fine more or less elegant approach; if i can i'd like to avoid things like "stripping off function name and parentheses'"
Help is much appreciated.
You cannot do what you want, at least the way you want to.
Regular expressions are technically a type 3 grammar and cannot describe recursive languages; your math expressions can contain other math expressions.
You could do something along the line of what you say you don't want to do. For example you could match an expression containing just a single pair of balanced parentheses, so in AVERAGE(G20,G21,MIN(G30,G31)) you could match MIN(G30,G31). If you then replaced the match by a marker and matched again you could match the next level, etc. But this not a good way to do it.
General math expressions can be described by a type 2 grammar, and can be easily parsed using a recursive descent parser. Such parsers are very easy to write. Essentially you write down the grammar you wish to parse and then write a function for each production. Google will get you started down this route.
If you don't want to write the parser yourself you can use a parser generator, in which case you still need to write the grammar, or search for one of the math expression libraries.
HTH
Hope you can help me, I am a bit of a newbie and have progressed without needing help but now I am stuck.
Firstly could somebody please tell me what this is below, I think it is ASCII but maybe wrong???
"\0\u{01}\0\0\0\u{1E}\0\0\u{02}\u{06}\0ÿ\0\0\0\0\0\u{02}.\u{01}(\0\t\0ü\0\u{07}\0\u{04}"
Secondly how would I convert this to an NSArray so I could get iterate through it and get a value at a given location in the array?
I am using Swift
Many thanks in advance
Charles
As mentioned above, your string looks quite weird, and the \u{xx} values tell Swift to replace that part with the unicode character corresponding to hexadecimal value xx (see unicode-table).
I do not know why you would want to convert it to the Objective-C type NSArray while you could just use the Swift type Array. By that, I mean you can generate an array of the string by using var strArray = Array(str), where your string is stored in str. From that point on, you can use the following code to iterate over the contents of the string:
for var char in strArray {
//do something with char
}
char then loops through all indices of the strArray, holding the value at the current index.
I'm trying to look at a string and reject anything that has seq= or app= in the string. Where it gets tricky is I need elements with q=something or p=something.
The seq= part of the string is always preceded an & and app= is always preceded by a ?
I have absolutely no idea where to start. I've been using http://www.rubular.com/ to try and figure it out but to no avail.
Any help would be hugely appreciated.
Based on your question, I believe you could just reject any strings that match the following expression:
[\?&](?:seq|app)=
This will match any string that contains a ? or & followed by either app= or seq=. The ?: inside the parentheses just tells the regular expression not to bother to capture matching groups as sub-matches. They're not really necessary, but what the heck.
Here's a Rubular link with some samples.