Evaluating a table of functions at a point in mathematica - wolfram-mathematica

I would like to know if it is possible to evaluate a table of functions at a point in mathematica.
Right now I am given a table of 10 functions and would like to evaluate them at x = 0.
I tried this :
Evaluate[myTable, {x, 0}]
And it does not evaluate anything, it just gives this output:
Sequence[ {term1, term2, term3, term4, term5, term6, term7, term8, term9, term10}, {x,0}]
Replacing term1 ... term10 with the actual terms.
How would I be able to do this?
Thanks,
Bucco

Through[{f1,f2,f3,f4,f5,f6,f7,f8,f9,10}[0]]

Related

SWI-Prolog list result

I'm coding in prolog, but I have a issue. How can I make that the answer appears just once? for example I just want that "X = uni, X = uca, X = unam" but instead it just keep showing me the options repeatedly.
This is some of it:
is(uni, college).
is(uca, college).
is(unan, college).
is(computation, carrer).
In this part I'm assigning available places to the carrer
has(computation, available_places, 200).
and finally assigning the carrer to a college
offers(unan, computation).
offers(uni, computation).
offers(uca, computation).
and I make the query like this:
which(X):- is(X, college), is(Y, carrer), offers(X, Y),has(Y, available_places, Z), Z<300.
but the result as I said at the beggining, show me the names of the college repeadtedly. Any idea how to solve this? D:

A list of variables used by Wolfram Mathematica function

Is there a way to get a list of variables used by a function?
For example:
a=1;
b=2;
f[x_]:= 2a*x+b;
Needed:
SomeFunction[f]
Output:
{{x},{a,b}}
The parameters of the function ({x}) are not really mandatory.
Thanks.
To get all the symbols (not necessarily variables) you could start with something like this:
DownValues[f]
which yields:
{HoldPattern[f[x_]] :> 2 a x + b}
The problem is then processing this in a way that you don't let Mathematica do the substitution. This is done with Hold:
held=(Hold //# DownValues[f][[1]])[[1, 2]]
which yields:
Hold[Hold[Hold[2] Hold[a] Hold[x]] + Hold[b]]
You can extract all the stuff that looks like a symbol with:
Cases[held, Hold[_Symbol], Infinity]
and you get:
{Hold[a], Hold[x], Hold[b]}
To make this a little nicer:
Union[Flatten[Hold ## Cases[held, Hold[_Symbol], Infinity]]]
which gives you:
Hold[a, b, x]
You still need the Hold, because as soon as you lose it Mathematica will evaluate a and b and you'll lose them as symbols.
If you notice, it's considering x a symbol, which you may not want because it's a parameter. You can tease the parameters out of the left side of the DownValues[f][[1]] RuleDelayed (:>) expression, and extract them, but I'll leave this detail to you.

Parse To Prolog Variables Using DCG

I want to parse a logical expression using DCG in Prolog.
The logical terms are represented as lists e.g. ['x','&&','y'] for x ∧ y the result should be the parse tree and(X,Y) (were X and Y are unassigned Prolog variables).
I implemented it and everything works as expected but I have one problem:
I can't figure out how to parse the variable 'x' and 'y' to get real Prolog variables X and Y for the later assignment of truth values.
I tried the following rule variations:
v(X) --> [X].:
This doesn't work of course, it only returns and('x','y').
But can I maybe uniformly replace the logical variables in this term with Prolog variables? I know of the predicate term_to_atom (which is proposed as a solution for a similar problem) but I don't think it can be used here to achieve the desired result.
v(Y) --> [X], {nonvar(Y)}.:
This does return an unbound variable but of course a new one every time even if the logical variable ('x','y',...) was already in the term so
['X','&&','X'] gets evaluated to and(X,Y) which is not the desired result, either.
Is there any elegant or idiomatic solution to this problem?
Many thanks in advance!
EDIT:
The background to this question is that I'm trying to implement the DPLL-algorithm in Prolog. I thought it would by clever to directly parse the logical term to a Prolog-term to make easy use of the Prolog backtracking facility:
Input: some logical term, e.g T = [x,'&&',y]
Term after parsing: [G_123,'&&',G_456] (now featuring "real" Prolog variables)
Assign a value from { boolean(t), boolean(f) } to the first unbound variable in T.
simplify the term.
... repeat or backtrack until a assignment v is found so that v(T) = t or the search space is depleted.
I'm pretty new to Prolog and honestly couldn't figure out a better approach. I'm very interested in better alternatives! (So I'm kinda half-shure that this is what I want ;-) and thank you very much for your support so far ...)
You want to associate ground terms like x (no need to write 'x') with uninstantiated variables. Certainly that does not constitute a pure relation. So it is not that clear to me that you actually want this.
And where do you get the list [x, &&, x] in the first place? You probably have some kind of tokenizer. If possible, try to associate variable names to variables prior to the actual parsing. If you insist to perform that association during parsing you will have to thread a pair of variables throughout your entire grammar. That is, instead of a clean grammar like
power(P) --> factor(F), power_r(F, P).
you will now have to write
power(P, D0,D) --> factor(F, D0,D1), power_r(F, P, D1,D).
% ^^^^ ^^^^^ ^^^^
since you are introducing context into an otherwise context free grammar.
When parsing Prolog text, the same problem occurs. The association between a variable name and a concrete variable is already established during tokenizing. The actual parser does not have to deal with it.
There are essentially two ways to perform this during tokenization:
1mo collect all occurrences Name=Variable in a list and unify them later:
v(N-V, [N-V|D],D) --> [N], {maybesometest(N)}.
unify_nvs(NVs) :-
keysort(NVs, NVs2),
uniq(NVs2).
uniq([]).
uniq([NV|NVs]) :-
head_eq(NVs, NV).
uniq(NVs).
head_eq([], _).
head_eq([N-V|_],N-V).
head_eq([N1-_|_],N2-_) :-
dif(N1,N2).
2do use some explicit dictionary to merge them early on.
Somewhat related is this question.
Not sure if you really want to do what you asked. You might do it by keeping a list of variable associations so that you would know when to reuse a variable and when to use a fresh one.
This is an example of a greedy descent parser which would parse expressions with && and ||:
parse(Exp, Bindings, NBindings)-->
parseLeaf(LExp, Bindings, MBindings),
parse_cont(Exp, LExp, MBindings, NBindings).
parse_cont(Exp, LExp, Bindings, NBindings)-->
parse_op(Op, LExp, RExp),
{!},
parseLeaf(RExp, Bindings, MBindings),
parse_cont(Exp, Op, MBindings, NBindings).
parse_cont(Exp, Exp, Bindings, Bindings)-->[].
parse_op(and(LExp, RExp), LExp, RExp)--> ['&&'].
parse_op(or(LExp, RExp), LExp, RExp)--> ['||'].
parseLeaf(Y, Bindings, NBindings)-->
[X],
{
(member(bind(X, Var), Bindings)-> Y-NBindings=Var-Bindings ; Y-NBindings=Var-[bind(X, Var)|Bindings])
}.
It parses the expression and returns also the variable bindings.
Sample outputs:
?- phrase(parse(Exp, [], Bindings), ['x', '&&', 'y']).
Exp = and(_G683, _G696),
Bindings = [bind(y, _G696), bind(x, _G683)].
?- phrase(parse(Exp, [], Bindings), ['x', '&&', 'x']).
Exp = and(_G683, _G683),
Bindings = [bind(x, _G683)].
?- phrase(parse(Exp, [], Bindings), ['x', '&&', 'y', '&&', 'x', '||', 'z']).
Exp = or(and(and(_G839, _G852), _G839), _G879),
Bindings = [bind(z, _G879), bind(y, _G852), bind(x, _G839)].

Pattern matching types in Prolog

Let's say I have a list looking like this:
List=[alpha(1,2),beta(3,4),gamma(4,1)]
Ok, so I want to make a certain pattern matching here... I know I can do:
Try=alpha(Y,Z).
Try=alpha(1,2)
Y=1
Z=2
But I would like to do for example:
Try=X(Y,Z)
X=alpha
Y=1
Z=2
...so that I can pass on the data to another predicate:
targetPredicate(Type,Value1,Value2):-
Type=alpha
...
and then do something with it instead of having to make one help predicate for every type I might run into:
helpPredicate(Input):-
Input=alpha(Value1, Value2),
targetPredicateAlt(Value1, Value2).
helpPredicate(Input):-
Input=beta(Value1, Value2),
targetPredicateAlt(Value1, Value2).
helpPredicate(Input):-
Input=gamma(Value1, Value2),
targetPredicateAlt(Value1, Value2).
Is there any way to get around this or am I doomed to use a ton of help predicates?
You can use the univ predicate =../2:
Suppose you have Try=alpha(1,2), then
Try =..[Name, X, Y].
would yield Name = alpha, X = 1, Y = 2.

how to use units along function parameter values in Mathematica

I would like to pass the parameter values in meters or kilometers (both possible) and get the result in meters/second.
I've tried to do this in the following example:
u = 3.986*10^14 Meter^3/Second^2;
v[r_, a_] := Sqrt[u (2/r - 1/a)];
Convert[r, Meter];
Convert[a, Meter];
If I try to use the defined function and conversion:
a = 24503 Kilo Meter;
s = 10198.5 Meter/Second;
r = 6620 Kilo Meter;
Solve[v[r, x] == s, x]
The function returns the following:
{x -> (3310. Kilo Meter^3)/(Meter^2 - 0.000863701 Kilo Meter^2)}
which is not the user-friendly format.
Anyway I would like to define a and r in meters or kilometers and get the result s in meters/second (Meter/Second).
I would be very thankful if anyone of you could correct the given function definition and other statements in order to get the wanted result.
Here's one way of doing it, where you use the fact that Solve returns a list of rules to substitute a value for x into v[r, x], and then use Convert, which will do the necessary simplification of the resulting algebraic expression as well:
With[{rule = First#Solve[v[r,x]==s,x]
(* Solve always returns a list of rules, because algebraic
equations may have multiple solutions. *)},
Convert[v[r,x] /. rule, Meter/Second]]
This will return (10198.5 Meter)/Second as your answer.
You just need to tell Mathematica to simplify the expression assuming that the units are "possitive", which is the reason why it doesn't do the simplifications itself. So, something like
SimplifyWithUnits[blabla_, unit_List]:= Simplify[blalba, (#>0)&/#unit];
So if you get that ugly thing, you then just type %~SimplifyWithUnits~{Meter} or whatever.

Resources