Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I plan to write a program or rather function which will be able to analyze a string parameter which in turn will be math expression. Only the 4 basic operations are allowed(addition, subtraction, multiplication and division) and the numbers are all whole numbers from -100 to 100. The result is allowed to be float. I know the registries work in the same way I.e calculate result of two numbers and store it, than calculate result of stored value and the next operant and store. And so forth until there are no operands left. The number of operands will usually be 2 but I will have a need of 3 or even more so yes, more operands is a requirement.
I was wondering how would you structure this in C#? What tools helper functions you would use in this scenario?
Note: I am working on Unity 5.1.4 project and I want to use a math parser in it. Unity is .NET 2.0
Note: This seems most promising: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html
It uses a variant of eval() function.
In .NET there are no some high level helper functions to help you with this. You would have to parse and tokenize the string in your code. There are however third party libraries that do what you need, for instance Expression Compiler, Simple Math Parser, Mathos Parser, and many other. Search for math expression parser.
If you want to make one from scratch you could look the code of existing ones.
Hans Passant mentions a simple solution, maybe just what you need. You get the result of the expression, so if you need just that, and not the actual expression tokens, then .NET got you covered.
This tool finished the job with no adding external references, dlls or what not: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
There is a function like CalculateProfit(decimal a, decimal b, float c, TimeSpan d) and its each input parameter has minimum, maximum and initial value settings.
Its output is smooth but not linear, it has multiple peaks and falls. I want to bruteforce its inputs and find maximum possible output. How to optimize this without trying each possible combination? Maybe some kind of binary search?
I think the algorithm should use big delta steps at start to find most peaks and then tweak values with small deltas. Also I would bruteforce one input until I find best output and then try same for next inputs, then go back to tweaking first input and so on.
Update: the function is a complex algorithm which performs analysis on markets historical data (so it's not just a formula). Therefore I'm asking for some bruteforce optimizations, not trying to "solve" it as an equation.
You need to read about partial differential equations solvers of 2 or more variables.
https://math.oregonstate.edu/home/programs/undergrad/CalculusQuestStudyGuides/vcalc/min_max/min_max.html
Then you need to study one algorithm that can solve it, Finite Volume and Spectral Method are the most commonly used in Simulation.
https://en.wikipedia.org/wiki/Numerical_partial_differential_equations
You can find easy solutions on Matlab if you are interested in just solving your problem. C# can call Matlab functions with some setup.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create simple search engine for my application. Let's simplify it to the following: we have some texts (a lot) and i need to search and show relevant results.
I've based on this great article extend some things and it works pretty well for me.
But i have problem with stemming words to terms. For example words "annotation", "annotations" etc. will be stemmed to "annot", but imagine you try search something, and you will see unexpected results:
"anno" - nothing
"annota" - nothing
etc.
Only word "annot" will give relevant result. So, how should i improve my search to give expected results? Because "annot" contains "anno" and "annota" is slightly more than "annot". Using contains all the time obviously isn't the solution
If in first case i can use some Ternary search tree, in second case i don't know what to do.
Any ideas would be very helpful.
UPDATE
oleksii has pointed me to n-grams here, which may works for me, but i don't know how to properly index n-grams.
So the Question:
Which data structure would be the best for my needs
How properly index my n-grams
Stemming perhaps isn't much relevant here. Stemming will convert a plural to a singular form.
Given you have a tokeniser, a stemmer and a cleaner (to remove stop words, perhaps punctuation and numbers, short words etc) what you are looking at is a full-text search. I would advice you to get an off-the-shelf solution (like Elasticsearch, Lucene, Solr), but if you fancy a DIY approach I can suggest the following naive implementation.
Step 1
Create a search-orientated tokeniser. One example would be an n-gram tokeniser. It will take your word and split into the following sequences:
annotation
1 - [a, n, o, t, a, i]
2 - [an, nn, no, ot, ...]
3 - [ann, nno, not, ota, ...]
4 - [anno, nnot, nota, otat, ...]
....
Step 2
Sort n-grams for more efficient look-up
Step 3
Search n-grams for exact match using binary search
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm wondering if there exists a library (preferably in .NET) for generating outputs based on formal grammars.
I'm talking about this:
https://en.wikipedia.org/wiki/Chomsky_hierarchy#Formal_grammars
So you give it the grammar as in:
(Non-terminal symbols, Terminal symbols, Production rules, Start symbol)
so:
Grammar = ({E,A,B}, {(,),a,+,*}, P, E);
Where
P = {
E -> A,
E -> E + A,
A -> B,
A -> A * B,
B -> a,
B -> (E)
};
And then the library would produce example random outputs of that grammar like:
a * a + a
a + (a + a)
a * a * a
What I'm going to do after either creating or finding a library like this is to then change the way it produces the output to make it more biased to produce certain examples more than others, so in the previous example I can make it more biased to produce more expressions with addition in them rather than multiplication.
So basically assign weights to production rules to make some of them more likely to get applied when there is more than one valid.
So my main question is, is there a library out there that can do the first step which is producing random outputs based on a formal grammar like my example?
Update
Thinking about this problem again, I have a few more notes:
One of the problems here is to know when to stop applying production rules. Either make sure that the grammar does stop at some point or devise a way in the algorithm/code that makes it less likely to keep expanding the expression further on.
I realise now that this might be too theoretical and there is probably a more practical way. I mean, there have to be some tools out there that essentially do this same thing but maybe just use a different syntax or approach that is more specific to certain problems or maybe something more flexible.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I can't find for the life of me the correct names for interpolations like the ones below.
I am trying to look them up google using ease-in interpolation, types of interpolation, but without much luck.
All I want is to implement them in a fashion like this:
double Interp ( double value, double t )
where value is the value to be interpolated and t is the time value that can be any value between 0-1, including 0 and 1.
So if Interp was using a linear interpolation and value was 10, and t was 0.5, the return would be 5. But I want to get the values using other interpolations.
Any help on this?
I remember seeing a website with flash animations showing the formula of each one time but can't find it anymore.
I've heard this referred to as "easing" or "tweening".
http://robertpenner.com/easing/ has a Flash demo and links to a PDF file with the equations.
http://code.google.com/p/tweener/ has an ActionScript library to implement them, with links to ports in JavaScript, Python, C++, etc.
http://gsgd.co.uk/sandbox/jquery/easing/ is an easing plugin for jQuery.
Interpolation involves estimating a curve based on a set of inputs, the larger the set of inputs the better the curve estimation. Is this what you are trying to do here? This guys talks about linear and quadratic interpolation techniques. http://www.codeproject.com/KB/recipes/simple_interpolation.aspx. If you want some more specialised interpolation techniques we really need to know a little more about the shape of the curve you are trying to estimate
See: http://mathworld.wolfram.com/Interpolation.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is it possible to convert a string expression into a boolean condition?
For example, I get the following string:
var b = "32 < 45 && 32 > 20"
I would like to create a bool expression out of this and invoke it. The string representation is also flexible (to make it more fun), so it allows ||, &&, ().
Have a look at Flee (Fast Lightweight Expression Evaluator) on CodePlex.
I would use Irony, the .NET language kit. You could construct a simple grammar with Irony and then parse the string into executable command. There's a decent example of an arthmetic grammar in this tutorial and in the Expression Grammar Sample, its a pretty common request ;)
I definitely suggest using a proper compiler as opposed to Regex or a roll your own approach - it will be much more extensible if you ever want to add more rules.
If it follows all C# expression rules then compile it as dynamic code as per http://www.west-wind.com/presentations/dynamiccode/dynamiccode.htm
If you're dealing with relatively simple mathematical expressions then a straightforward implementation of the shunting-yard algorithm should do the trick.
Take a look at my library, Proviant. It's a .NET Standard library using the Shunting Yard algorithm to evaluate boolean expressions. You could also implement your own grammar.
I think creating an interpreter for this string would not take too long time.
http://www.industriallogic.com/xp/refactoring/implicitLanguageWithInterpreter.html
here you can find information about design that can be used to create it.
You could take a look at JINT (Javascript Interpreter for .NET) http://jint.codeplex.com/