Related
I´m making a one bit addition:
sumbit(CIN,A,B,CO,R):- ...
?- sumbit(0
,1
,1
,CO
,R)
,write(CIN),nl
,write(A),nl
,write("+"),nl
,write(B),nl
,write("--"),nl
,write(CO),write(R),nl.
What I want to do is to print the variable values of CIN,A,B,CO and R.
It should come out something like this:
0
1
+
1
--
10
Instead it comes out as this:
_40
_73
+
_149
--
10
Yes.
Also is there a way to not print the "Yes"?
I´m using strawberry prolog if it helps.
Thank you in advance
One way to achieve that without altering your predicate definition is to tweak the query, like so:
?- [CIN, A, B] = [0, 1, 1]
,sumbit(CIN
,A
,B
,CO
,R)
,write(CIN),nl
,write(A),nl
,write("+"),nl
,write(B),nl
,write("--"),nl
,write(CO),write(R),nl.
Now all variables are instantiated, either by the call itself, or prior to the call.
When a variable is not instantiated, there's no value to print, so its "name" is printed instead. But since non-used name has no meaning in itself, it can be freely renamed by the system to anything. In SWI Prolog:
1 ?- write(A).
_G1338
true.
The renaming is usually done, as part of the Prolog problem solving process, to ensure that any two separate invocations of the same predicate do not interfere with each other.
So where SWI Prolog uses names like _G1338, the Prolog implementation you're using evidently uses names with the numbers only, after the underscore, like _40.
I found an answer by putting the write() inside the sumbit(...) predicate:
sumbit(CIN,A,B,CO,R):-
xor_(A,B,R1)
,and(A,B,R2)
,xor_(R1,CIN,R)
,and(R1,CIN,R4)
,or(R2,R4,CO)
,write(CIN),nl
,write(A),nl
,write("+"),nl
,write(B),nl
,write("--"),nl
,write(R),nl.
There are still some unanswered questions though:
is there a way to not print the "Yes"?
what was the _number that came out before?
I can't get the syntax to do what I want, and I am now not sure if it is even possible.
small review:
One can do this:
{Slider[Dynamic[b], {-2 Pi, 2 Pi}], Dynamic[Sin[b]]}
and now each time the slider moves, 'b' changes, and its Sin[] is automatically printed
But suppose I want to do the computation (Sin[]) directly where the slider is and only show the final result of Sin[], then I can use the second argument of Dynamic like this:
{Slider[Dynamic[b, (b = #; a = Sin[b]; #) &], {-2 Pi, 2 Pi}],
Dynamic[a]}
Now I want to use Manipulate, and do the same thing. I can do the same as the first example above like this:
Manipulate[
Sin[b],
Control[{{b, 0, "b="}, -2 Pi, 2 Pi, ControlType -> Slider}]
]
In the above, Manipulate took care of the 'Dynamic' stuff, and updated Sin[b] whenever 'b' changes.
Now, I want to see if I can do the second case using Manipulate, so I can write:
Manipulate[
a,
Control[{{b, 0, "b="}, -2 Pi, 2 Pi, ControlType -> Slider}] (*where to insert Sin[b]?*)
]
('a' has to initialized to some value for initial display).
But I am not able to figure how to use the second argument for 'b' in the above. Can't figure the syntax, and now sure if it possible?
of course, one can't just write
Manipulate[
a,
{Slider[Dynamic[b, (b = #; a = Sin[b]; #) &], {-2 Pi, 2 Pi}]}
]
This is not even valid syntax of Manipulate controls.
Question is: Is it possible to use second argument of Dynamic in setting up Manipulate controls?
The reason I am asking, is that it could make it easier to 'localize' computation right there, where the control variable changes, and only show the final result elsewhere. Like a local callback function in a way, where computation related to changes for each control sits right next to where the control is.
thanks
Update 9/16/11
Well, after a month of struggle, finally I have the Mathematica CDF up and running.
Thanks to the help of the trick shown here by Simon, and others who answered my questions while I was doing this CDF (Leonid, Heike, WReach, Belisarius and others).
The few tricks I learned here helped finish this demonstration, here is a link if someone wants to try it.
The design of this CDF is different from everything I've done before. It is based on finite state machine. There is only ONE tracked symbol in the whole CDF. Using the second argument of dynamics, the event name is recorded, and in the main expression of Manipulate, which runs the finite state machines, it has 5 states it can be in, and there are 8 events. Depending on the current state and the current event that just happened, it switches to new state (or can stay in the same state, depending), and the main display is updated. No trigger needed. The state machine runs as fast as you allow it, controlled only by the time step size.
This simplifies the logic greatly, and makes it possible to handle make much more advanced UI and inter-dependent logic, since everything now runs in a well controlled way, and all the logic is in one place.
Without being able to set the event using the second argument of dynamics, this whole approach would not have been possible.
I need to write a note on this method to make it more clear.
So, just wanted to thank everyone here. I am now finishing another CDF using this method, a triple pendulum simulation.
Not all of the arguments of Manipulate after the first one have to be Control objects. You can put anything you like there - including completely customized dynamic controls. So, how about something like
Manipulate[a, {a, None}, {b, None},
Row[{"b= ",Slider[Dynamic[b, (b = #; a = Sin[b]; #)&], {-2 Pi, 2 Pi}]}]]
Where the {a, None} and {b, None} ensure that the variables are local, but aren't strictly necessary.
I am just getting started with pattern in mathematica. I want to know what the different ways to force mma to show -1+a as a-1. Many thanks!
The simplest way is probably -1 + a // TraditionalForm.
One due to Jean-Marc Gulliet (MathGroup)
(You may also be interested in the reply of Jens-Peer Kuska to this post)
PolynomialForm[-1 + a, TraditionalOrder -> True]
Out[34]= a-1
(PolynomialForm is undocumented, as far as I know. I am using Mma 7.)
You could probably use a hack like this
$PrePrint = (# /. -1 + expr__ :> Interpretation[Row[{expr, -1}], expr - 1]) &
But (as WReach suggests) it might be best to use the default Mathematica ordering of expressions and use TraditionalForm when you want it to look more like what a human would write.
I have been trying to decipher what this output means, but I just can't seem to figure it out. Does anybody know what is going on here?
I have even tried running the lines one by one and the errors only show up when the final line (show) is executed.
Stepping through the lines individually by themselves would not show you what is going on, you have to take apart the statement that is giving you the trouble: in this case, Show[p1, p2[1,1]. By themselves, neither p1 nor Show should give you trouble, which leads to the conclusion that it must be p2[1,1]. This is born out by running that by itself, which generates the same error.
This generates an error because of how Plot, Plot3D, etc evaluate the function argument. In general, they essentially do a Replace on the text of the function and may not expand function calls. A simple fix is to rewrite p2 as
p2[x0_, y0_] := Plot3D[Evaluate[p[x, y, x0, y0]], {x, 0, 2}, {y, 0, 2}]
which gets rid of the errors. Evaluate ensures that function is evaluated symbolically before Plot3D gets a hold of it avoiding any mishandling. I wish I had a better idea of when to use Evaluate in these cases, but if in general if you are getting errors from a plotting function like these, then it is most likely mishandling the function.
My mathematica output is
-0.751988 - 0.0708732 Log[e] - 0.0140273 Log[e]^2
But I want mathematica to calculate this expression for me i.e. set Log[e] = 1 and sum the terms. How do I instruct it to do that. I'm assuming it must be treating the functions as complex??
Regards
You probably wanted E or \[ExponentialE] which has the input alias ⋮ee⋮.
Built-in symbols are capitalized, so constants like pi, e are written as Pi, E
If i remember correctly, Mathematica use E as the Neper number, not e. I think you mispelled it, so Log[e] is not expanded.
If not so, with the substitution Log[e]->1 you can achieve what you want.