|
i have a mathematical function,example x^2+sin(x),but it is String.i want it is mathematical function,it can return value float
leo
|
|
|
|
|
nta_3886 wrote: i want it is mathematical function,it can return value float
Ok.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
use a stack to convert the formula into Reverse Polish Notation (RPN). Then evaluate the RPN version. You can find many examples of how to do both steps of this via google.
--
If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.
|
|
|
|
|
dan neely wrote: use a stack to convert the formula into Reverse Polish Notation (RPN). Then evaluate the RPN version. You can find many examples of how to do both steps of this via google.
Semi-on-topic... I just found my code for this from Pascal class back in the 80's. I used it on my first job to build an RPN based spreadsheet program in RPG II.
_________________________
Asu no koto o ieba, tenjo de nezumi ga warau.
Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
|
|
|
|
|
a lot of work
Russell
|
|
|
|
|
If you are using the .net Framework then you can compile that into a method in a temporary assembly and execute it as normal code.
I'm sure you can find a sample about that in the WWW.
-^-^-^-^-^-
no risk no funk ................... please vote ------>
|
|
|
|
|
|
If you are using Rexx then
interpret say "string"
Except you first need to write sin(x) and other functions you use in Rexx. You also need to use Rexx rules for syntax.
Rexx can compile and execute on the fly.
With just a little more detail in dos one might type
rexx math 1.2*4.7
5.64 prints
The file math.cmd contains
/* execute incomming statement */
numeric digits 40
arg cmd
interpret say cmd
It's been a while but saving x to a environment variable or some such is not that hard.
My 40 digit ln() in ln.cmd is
/* ln(x) */
numeric digits 40
arg x
/* prescale to put x into reasonable range */
xp = 0
if x > 2 then do until x < 2
xp = xp + 1
x = x / 2
end
/* now that x <=2 compute ln(x)
method from Abramowitz etal section 4.1.39
ln(2) to 40 places takes 26 loops */
z = x-1
val = 0
do t = 26 to 1 by -1
t2z = t*t*z
val = t2z / (t+t+val+1)
val = t2z / (t+t+val)
end
val = z / (val+1)
/* account for prescale */
if xp = 0 then return val
return (const(ln2)*xp) + val
|
|
|
|
|
Hello,
Suppose that we have a NxN square filled with letters. Which algorithm is the best to find any words starting with NiNj and continue from the starting point through adjacent squares (left-right-cross) where the word is in any length L?
Thanks and best regards..
.:: Something is Wrong ::.
|
|
|
|
|
I wrote a decent 5x5 Boggle game and felt that the problem was the dictionary. So...I kept multiple partial matches going always working the partial match with the lowest lexical value.
Finds game buried in some oddball directory... Discovers it does not work in xp. Posting an old result...
A L E R E
O O T S T
T G L U T
N Z O Y E
S C E N F
alert
alertest
alertly
ales
aloe
alter
alto
alts
colog
cols
colt
colter
coly
cone
coney
cony
coulter
couter
cozen
cozey
enfetter
enol
erst
ester
eyen
eyne
fets
fetter
fetus
feus
fytte
glue
gluey
glut
gluten
goal
goer
goes
gone
gout
gouty
lest
lets
logo
loot
looter
lots
lotus
lust
luster
lustre
lusty
lute
nets
nett
netter
neuter
nolo
nous
oleo
oles
oots
ousel
oust
ouster
outer
outgo
outgoes
outre
outs
outsert
outset
oyez
reset
rest
result
retool
rets
retuse
scene
scone
scouse
scout
scouter
sere
sloe
slog
slot
slue
slut
stela
steno
stere
stereo
stey
stoa
stole
stool
strette
stutter
stye
suet
suety
sutler
tela
terse
test
tester
testy
toes
tola
tole
tolu
toluene
tool
tooler
toot
tooter
tootle
tootler
tote
toter
tots
trestle
tret
tuts
tutty
tyne
ulster
user
utter
yett
youse
zone
|
|
|
|
|
So ?
.:: Something is Wrong ::.
|
|
|
|
|
pashje wrote: Which algorithm is the best
It depends on the value of N and on the lenght of the dictionary (L)...
If N is too big (compared with L) the best way is to try to search every word of the dictionary on the NxN square.
Elseway (N small) try to extract every possible words, and then test if the word exist.
BTW I prefere in both cases to use the first method. It seemes easyer to be implemented.
Russell
|
|
|
|
|
Well both methods will do the job surely but what if N is a Z+ ? It may 3 but also 3000.. So the performance is an important issue..
Maybe a genetic algorithm or somemthing with trees will work well ? Any text about thiss issue will really help !;)
.:: Something is Wrong ::.
|
|
|
|
|
if N can change from 3 to 3000...well...I think that it is not possible to find an algorithm with always an High performance. You need to implement at least 2 algorithms: the program will use the first one if N<k and="" the="" other="" if="" n="">K.
The treshold K must be set during your test to find what algorythm is faster.
About genetic algorithms I think that this isn't the right way.
I think that you can perform a tree algorithm sorting the dictionary somehow to avoid to check every words.
Russell
|
|
|
|
|
I did my final year project on a scrabble game and i found that the best way to increase speed when matching words is by choosing an efficient data storage technique for your dictionary.
A tree structure is needed to solve this problem, a DAWG would probably be the best choice.
|
|
|
|
|
Hi everyone,
Could anyone kindly assist with some code (preferably Java) for the Travelling Salesman Problem that uses any of the following Heuristics
1. Tabu Search
2. Hill Climbing
3. GRASP (Greedy Randomized Adaptive Search Procedure)
4. Simulated Annealing
I need this to just text some problems.
Thanks
laremtj
Jesus Loves You, Give your Heart to Him Today
|
|
|
|
|
laremtj wrote: preferably Java
This is a Visual Studio/.NET Centric site, if in case you haven't noticed.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
I'm not sure this is the rigth place to post this question but it sounds like a math problem to me so...
Problem:
I need all the combinations of a set of numbers (to make it easy lets take 3 numbers, but eventualy it has to work for 15numbers and more)
the user will give in 3 number for example: 1 , 5 , 19
then the programme needs to give the following combinations:
1, 5, 19
1, 19, 5
5, 1, 19
5, 19, 1
19, 1, 5
19, 5, 1
I've been searching for some algoritme our other way to do this for several weeks now and just can't find it
any ideas on how I could achieve this??
If my help was helpfull let me know, if not let me know why.
The only way we learn is by making mistaks.
|
|
|
|
|
Hi,
if you want to list all combinations, recursion is what you need.
Build a method that takes a collection as an input, and let a for loop pick one element,
remove it, then call recursively with wath remains in the collection, until the collection
is empty; then return.
BTW: if your collection is ordered, and your method picks in that same order, the result will
be ordered too.
Warning: for n elements, the recursion depth will be n, the number of combinations n!
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Recursion is a bad thing. You can almost always remove recursion from an algorithm using a simple stack. Case in point, you can remove recursion from the suggested algorithm with a stack. Something like this:
1 on Stack
5 on Stack
19 on Stack
Start popping -> 1,5,19
19 on Stack
5 on Stack
Start popping -> 1,19,5
etc ...
HossFly
|
|
|
|
|
Hoss Fly wrote: Recursion is a bad thing
Nonsense! Recursion is simply one tool that any professional should consider. There may be performance issues in some applications, but often you can't beat it for simplicity and elegance. Recursive descent parsing is an excellent example.
Hoss Fly wrote: You can almost always remove recursion from an algorithm using a simple stack
So why not use the processor stack - i.e. recursion. If you use built-in support rather than roll your own stack the code may be cleaner and less error prone.
Unless the number of elements is large (in which case there is no point enumerating all n! permutations), recursion is a perfectly valid method of solution to consider.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
You made my point for me. You shouldn't use recursion if you do not know the recursion depth. Whereas with a stack it is never an issue. I can describe many a problem where one cannot KNOW the recursion depth in advance (Flood Fill, Point Insertion w/r to Triangulation, clipping, etc...)
I have fixed recurssion based algorithms in two commercial products. The crashes this type of logic causes is often very difficult to catch. Try finding it in millions of lines of code and you will have the same opinion.
It is probably ok for some text book or academic problems. problem is developers fail to realize the stack problem and take the easy approach. I advocate thinking stack first, and recursion second (or last resort, or never.)
HossFly
|
|
|
|
|
|
As Peter already told you, recursion has its merits; it is often abused, but in this case
it is the right approach. My last remark (the depth n and result count n!) was
intended to illustrate that: if you care to consume n! results, then a stack depth of n
would not be an obstacle.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
<br />
char[] harfler = new char[7];<br />
harfler[0] = tb1.Text.ToCharArray()[0];<br />
harfler[1] = tb2.Text.ToCharArray()[0];<br />
harfler[2] = tb3.Text.ToCharArray()[0];<br />
harfler[3] = tb4.Text.ToCharArray()[0];<br />
harfler[4] = tb5.Text.ToCharArray()[0];<br />
harfler[5] = tb6.Text.ToCharArray()[0];<br />
harfler[6] = tb7.Text.ToCharArray()[0];<br />
<br />
List<string> liste = new List<string>();<br />
<br />
for (int i = 0; i < harfler.Length; i++)<br />
{<br />
for (int j = (i + 1) % 7, x = 0; x < harfler.Length; x++)<br />
{<br />
if (i != j)<br />
{<br />
for (int k = (j + 1) % 7, y = 0; y < harfler.Length; y++)<br />
{<br />
if (k != i && k != j)<br />
{<br />
liste.Add(harfler[i] + "" + harfler[j] + "" + harfler[k]);<br />
for (int l = (k + 1) % 7, z = 0; z < harfler.Length; z++)<br />
{<br />
if (l != i && l != j && l != k)<br />
{<br />
liste.Add(harfler[i] + "" + harfler[j] + "" + harfler[k] + "" + harfler[l]);<br />
for (int m = (l + 1) % 7, t = 0; t < harfler.Length; t++)<br />
{<br />
if (m != i && m != j && m != k && m != l)<br />
{<br />
liste.Add(harfler[i] + "" + harfler[j] + "" + harfler[k] + "" + harfler[l] + "" + harfler[m]);<br />
for (int n = (m + 1) % 7, v = 0; v < harfler.Length; v++)<br />
{<br />
if (n != i && n != j && n != k && n != l && n != m)<br />
{<br />
liste.Add(harfler[i] + "" + harfler[j] + "" + harfler[k] + "" + harfler[l] + "" + harfler[m] + "" + harfler[n]);<br />
for (int o = (n + 1) % 7, q = 0; q < harfler.Length; q++)<br />
{<br />
if (o != i && o != j && o != k && o != l && o != m && o != n)<br />
{<br />
liste.Add(harfler[i] + "" + harfler[j] + "" + harfler[k] + "" + harfler[l] + "" + harfler[m] + "" + harfler[n] + "" + harfler[o]);<br />
<br />
}<br />
<br />
o = (++o) % 7;<br />
}<br />
}<br />
<br />
n = (++n) % 7;<br />
}<br />
}<br />
<br />
m = (++m) % 7;<br />
}<br />
}<br />
<br />
l = (++l) % 7;<br />
}<br />
}<br />
<br />
k = (++k) % 7;<br />
}<br />
}<br />
<br />
j = (++j) % 7;<br />
}<br />
}<br />
Surely it is not an efficient way but in a specisific project this has worked well for me. It combinates 7 letters and produce words at least with length 3 and at most length 7.
.:: Something is Wrong ::.
|
|
|
|