"Private" symbols for sum(), diff(), or integrate() - symbolic-math

Let's consider the following function I've already mentioned in my previous question:
rot[i](f) := sum(sum(sum(sum(
G[r,i]*G[q,j]*W[i,j,k]*('diff(f[k], y[q]) + sum(K[k,q,m]*f[m], m, 1, N)),
r, 1, N),
j, 1, N),
k, 1, N),
q, 1, N) $
It kind of works in general, but what if original expression f already contains symbols r, j, and so on? In this case it doesn't do the right thing. The only solution I've found so far is to use some unique prefix for these symbols or at least start their names with underscores: r → __r, j → __j. But I hope there should be a more idiomatic solution. Is there anything?

Unfortunately, in Maxima all symbols are effectively global -- the x in some expression is the same symbol as the x in f(x) := .... So there is no general way to ensure that function arguments are distinguished from other symbols of the same name. Some functions attempt to make their arguments local, e.g., sum treats the index variable specially, but integrate does not.
I made an attempt to make a lexical block construct which would make it possible to distinguish symbols of the same name. It is called blex and I think a web search should find it, if not, let me know and I'll post a link.

Related

SciPy optimize over vector indeterminate

I want to define a function as follows:
def f(x,m):
return np.exp(((x[i]-m).T)#((x[i]-m))
where the input is a known dataset array x, for example
x = np.array([[1,2],[3,4]])
but also an unknown 2d vector m.
At the moment I cannot define properly this function, since I get the ERROR:
NameError: name 'm' is not defined
The truth is I don't know what is the correct way to define m. Should it be something like
m = []
maybe? I know that unlike mathematica, I need to tell something to Python regarding m but it is not clear to me what.
Most importantly, I am interested in adding all components of x and minimizing the logarithm of f(x,m) to find the value of m for which f(x,m) is minimum.
To minimize a function you can use the scipy.optimize.minimize function link.
This might help you in understanding how to work with this function
import numpy as np
from scipy.optimize import minimize
def f(m, x, i):
return np.exp(((x[i]-m).T)#((x[i]-m)))
x = np.array([[1,2],[3,4]])
i = 0
m = minimize(f, x0=(0,0), args=(x, i)).x
I changed the order of parameters of your function f. minimize expects the first parameter to be the "variable" parameter which in this case is m, x and i are kept constant during the optimization.
In the call of the minimize function, x0 is the initial guess of m which is important to give because it tells the minimize function the shape of m. args is used to input the "constant" parameters.
The function returns an OptimizeResult where now the x attribute is the best estimate for m. However, the OptimizeResult contains some more useful information about the optimization.

Breaking between/3 based "loop" in SWI-Prolog while maintaining choice points that follow it

I need to iterate over a range of numbers (e.g. using between/3) from 1 to Length where Length is a length of a given list. Iterated number, say N, is then applied to a predicate, say do_something until it stops failing. That is, do_something searches for a correct N after which all choice points of between/3 must be dropped. However, all choice points of do_something with proper N applied must be executed.
Fist attempt was something like this:
% main(+InputList, -N, -OutputList)
main(InputList, N, OutputList) :-
length(InputList, Length),
between(1, Length, N),
% cut all between/3 choice points as soon as Num is instantiated
freeze(Num, !),
do_something(InputList, N, OutputList),
Num is N.
This did not work as freeze/2 doesn't cut between/3 choice points. A version with when/2 did not work either. I gather, it is because both freeze/2 and when/2 are executed in a separate thread and do not affect the main thread where between/3 is.
After a while I ended up with the following code that does what's needed, but is inefficient:
main(InputList, N, OutputList) :-
length (InputList, Length),
between(1, Length, N),
do_something(InputList, N, _),
% cut all prior choice points as soon as proper N is found
!,
% start do_something over!
do_something(InputList, N, OutputList).
Inefficiency is that do_something's choice point with proper N is executed twice. First just before cut, and then once again right after it.
Though this solution works in my case, it is not applicable to a generic case when all do_something choice points must be executed only once.
Note that N is required to be minimum out of 1..Length range and is not known in advance. Hence searching for it with do_something.
Is there a better solution to this? Is there a way to implement a predicate similar to between/3 that can stop when signaled somehow? Could there be a specialized builtin predicate that does what's needed? Any fruitful ideas are highly appreciated.
There is another possibility using *->/2, which is a variant of ->/2 that does not kill the choice point of the condition. Now we are not there was we want to kill an older choicepoint. I don't know whether there are Prolog systems that can do so. Most have provisions to kill all choice points since some mark, but I'm not aware of one that kills a specific one. So, we must insert a bit of code to conditionally stop further processing. This results in:
main(InputList, N, OutputList) :-
length(InputList, Length),
State = state(cont),
between(1, Length, N),
( State = state(cont)
-> true
; !,
fail
),
( do_something(InputList, N, OutputList)
*-> nb_setarg(1, State, stop)
; fail
).
This is totally non-portable, although many systems have *-> (sometimes named if/3) and many have some form of non-backtrackable assignment while if you are desperate you can use assert/retract for that.
See online at SWISH
Paulo's answer is certainly more portable. This should be way faster though and does not evaluate all solutions of do_something before returning the first, neither does it evaluate do_something twice.
Hoping that I understood your problem description:
main(InputList, N, OutputList) :-
length(InputList, Length),
between(1, Length, N),
findall(
OutputList0,
do_something(InputList,N,OutputList0),
OutputLists
),
% cut all prior choice points as soon as proper N is found
OutputLists = [_|_],
!,
member(OutputList, OutputLists).
The findall/3 call will return Solutions = [] until the do_something/3 predicate succeeds. When that happens, the findall/3 call ensures that, for that value of N, all the choice points of do_something(InputList, N, OutputList) are visited. The following cut then fixes the value of N and you can go from there.
P.S. Updated with the change you describe in your comment to make it work for your case. There are some non-portable hacks to find only a given number of solutions if you don't want to collect them all.
It turns out that between/3 is a distraction. We don't require it and thus a simple, efficient, and portable solution is possible:
main(InputList, N, OutputList) :-
length(InputList, Length),
Length >= 1,
main(1, Length, InputList, OutputList).
main(N, Length, InputList, OutputList) :-
( do_something(InputList, N, OutputList) *->
true
; N < Length,
M is N + 1,
main(M, Length, InputList, OutputList)
).
As in Jan’s solution, it does not evaluate all solutions of do_something/3 before returning the first, neither does it evaluate the predicate twice. But it also doesn’t require the nasty and non-portable nb_setarg/2 predicate tricks.
Note that, as Jan remarked, the soft-cut control construct, *->/2, or its if/3 meta-predicate variant, can be found in several Prolog systems (including, in one form or the other, CxProlog, Ciao Prolog, ECLiPSe, GNU Prolog, JIProlog, SICStus Prolog, SWI-Prolog, and YAP).
P.S. I keep my first answer as it's more portable and exemplifies a pattern that may be useful for other problems.

Can someone explain what are strong updates and give an example which illustrates it?

I'm reading the Strong Update section of a paper on Efficient Context-Sensitive Pointer Analysis for C Programs and I'm not able to grasp exactly what it means. Can someone provide an example, especially for this line in the link:
This greatly improves our ability to perform strong updates. Since a heap block represents all the storage allocated in a particular context, we assume that locally allocated heap blocks are never unique.
Strong update, weak update
In the context of static analysis, where one is trying to infer all possible behaviors of the program at once, a strong update is an update (assignment) operation in which the address being updated is known precisely. By contrast, an assignment where the address written to is not known precisely is called a weak update.
When handling a weak update, not only the new value can be written at several locations and it is not known which, but in addition, the possibility must be considered that each location keeps its old value (since the update may happen elsewhere).
Example
Consider Frama-C's value analysis, an efficient context-sensitive pointer analysis for C programs available as a package in most Linux distributions. Let us say that we are analyzing the following program:
int a, b, c, d, *p, t[5];
int main(int argc, char **argv){
a = 1; // strong
p = &b;
*p = 2; // strong
if (c & 1)
p = &c;
else
p = &d;
*p = 3; // weak
t[2] = 4; // strong
t[c & 2] = 5; // weak
}
When analyzing this example with Frama-C's value analysis, one gets:
$ frama-c -val t.c
[value] Values at end of function main:
a ∈ {1}
b ∈ {2}
c ∈ {0; 3}
d ∈ {0; 3}
p ∈ {{ &c ; &d }}
t[0] ∈ {0; 5}
[1] ∈ {0}
[2] ∈ {4; 5}
[3..4] ∈ {0}
__retres ∈ {0}
The locations c, d, t[0] and t[2] have been the target of a weak update. They each can contain either a new value (that could have been written there) or an old value (that was there at the time and could have remained).
By opposition, a and b have been the object of a strong update. It was known that the assignment was writing exactly to each of these variables, so there is no need to consider the possibility they might have kept their old values.
In the context of the article
Regarding the precise paragraph that you cite:
The key is to recognize that an extended parameter representing the
initial value of a unique pointer can be a unique block even if that
pointer has many possible values in the calling context. Since the
pointer can only contain one of those possibilities at any one time,
the extended parameter is a unique block within the scope of the
procedure. Only when more than one location points to an extended
parameter and the actual values for that parameter are not a single
unique location must we mark the parameter as not unique. This greatly
improves our ability to perform strong updates.
The researchers aim at using the strong update as often as possible, since it is more precise. In this paragraph, they remark that although a pointer p may point to several possible locations, if you give a name to “the location p points to”, then you can strongly update that location. I think this is what they are saying.
This would allow, in my example program, to read from *p at the end of the program and to find that it contains exactly 3, although p points to c that may contain 0 or 3 or to d that may contain 0 or 3. Older versions of Frama-C's value analysis inferred this information with a technique similar to that described (if I understand it correctly) but it was too expensive and was removed.

Is it safe to pass GEMV the same output- as input vector to achieve a destructive matrix application?

If A is an n x n matrix and x a vector of dimension n, is it then possible to pass x to GEMV as the argument to both the x and y parameter, with beta=0, to achieve the operation x ← A ⋅ x ?
I'm specifically interested in the Cublas implementation, with C interface.
No. And for Fortran it has nothing to do with the implementation - In Fortran it breaks the language standard to have aliased actual arguments for any subprogram as it breaks the language standard unless those arguments are Intent(In). Thus if the interface has dummy arguments that are Intent(Out), Intent(InOut) or have no Intent you should always use separate variables for the corresponding actual arguments when invoking the subprogram.
NO.
Each element of the output depends on ALL elements of the input vector x
For example: if x is the input and y is the output, A is the matrix,
The ith element of y would be generated in the following manner.
y_i = A_i1*x_1 + A_i2 * x_2 ... + A_in * x_n
So if you over-write x_i with the result from above, some other x_r which depends on x_i will not receive the proper input and produce improper results.
EDIT
I was going to make this a comment, but it was getting too big. So here is the explanation why the above reasoning holds good for parallel implementations too.
Unless each parallel group / thread makes a local copy of the original data, in which case the original data can be destroyed, this line of reasoning holds.
However, doing so (making a local copy) is only practical and beneficial when
Each parallel thread / block would not be able to access the
original array without significant amount of over-head.
There is enough local memory (call it cache, or shared memory or even
regular memory in case of MPI) to hold a separate copy for each
parallel thread / block.
Notes:
(1) may not be true for many multi-threaded applications on a single machine.
(1) may be true for CUDA but (2) is definitely not applicable for CUDA.

ForEach loop in Mathematica

I'd like something like this:
each[i_, {1,2,3},
Print[i]
]
Or, more generally, to destructure arbitrary stuff in the list you're looping over, like:
each[{i_, j_}, {{1,10}, {2,20}, {3,30}},
Print[i*j]
]
Usually you want to use Map or other purely functional constructs and eschew a non-functional programming style where you use side effects. But here's an example where I think a for-each construct is supremely useful:
Say I have a list of options (rules) that pair symbols with expressions, like
attrVals = {a -> 7, b -> 8, c -> 9}
Now I want to make a hash table where I do the obvious mapping of those symbols to those numbers. I don't think there's a cleaner way to do that than
each[a_ -> v_, attrVals, h[a] = v]
Additional test cases
In this example, we transform a list of variables:
a = 1;
b = 2;
c = 3;
each[i_, {a,b,c}, i = f[i]]
After the above, {a,b,c} should evaluate to {f[1],f[2],f[3]}. Note that that means the second argument to each should be held unevaluated if it's a list.
If the unevaluated form is not a list, it should evaluate the second argument. For example:
each[i_, Rest[{a,b,c}], Print[i]]
That should print the values of b and c.
Addendum: To do for-each properly, it should support Break[] and Continue[]. I'm not sure how to implement that. Perhaps it will need to somehow be implemented in terms of For, While, or Do since those are the only loop constructs that support Break[] and Continue[].
And another problem with the answers so far: they eat Return[]s. That is, if you are using a ForEach loop in a function and want to return from the function from within the loop, you can't. Issuing Return inside the ForEach loop seems to work like Continue[]. This just (wait for it) threw me for a loop.
I'm years late to the party here, and this is perhaps more an answer to the "meta-question", but something many people initially have a hard time with when programming in Mathematica (or other functional languages) is approaching a problem from a functional rather than structural viewpoint. The Mathematica language has structural constructs, but it's functional at its core.
Consider your first example:
ForEach[i_, {1,2,3},
Print[i]
]
As several people pointed out, this can be expressed functionally as Scan[Print, {1,2,3}] or Print /# {1,2,3} (although you should favor Scan over Map when possible, as previously explained, but that can be annoying at times since there is no infix operator for Scan).
In Mathematica, there's usually a dozen ways to do everything, which is sometimes beautiful and sometimes frustrating. With that in mind, consider your second example:
ForEach[{i_, j_}, {{1,10}, {2,20}, {3,30}},
Print[i*j]
]
... which is more interesting from a functional point of view.
One possible functional solution is to instead use list replacement, e.g.:
In[1]:= {{1,10},{2,20},{3,30}}/.{i_,j_}:>i*j
Out[1]= {10,40,90}
...but if the list was very large, this would be unnecessarily slow since we are doing so-called "pattern matching" (e.g., looking for instances of {a, b} in the list and assigning them to i and j) unnecessarily.
Given a large array of 100,000 pairs, array = RandomInteger[{1, 100}, {10^6, 2}], we can look at some timings:
Rule-replacement is pretty quick:
In[3]:= First[Timing[array /. {i_, j_} :> i*j;]]
Out[3]= 1.13844
... but we can do a little better if we take advantage of the expression structure where each pair is really List[i,j] and apply Times as the head of each pair, turning each {i,j} into Times[i,j]:
In[4]:= (* f###list is the infix operator form of Apply[f, list, 1] *)
First[Timing[Times ### array;]]
Out[4]= 0.861267
As used in the implementation of ForEach[...] above, Cases is decidedly suboptimal:
In[5]:= First[Timing[Cases[array, {i_, j_} :> i*j];]]
Out[5]= 2.40212
... since Cases does more work than just the rule replacement, having to build an output of matching elements one-by-one. It turns out we can do a lot better by decomposing the problem differently, and take advantage of the fact that Times is Listable, and supports vectorized operation.
The Listable attribute means that a function f will automatically thread over any list arguments:
In[16]:= SetAttributes[f,Listable]
In[17]:= f[{1,2,3},{4,5,6}]
Out[17]= {f[1,4],f[2,5],f[3,6]}
So, since Times is Listable, if we instead had the pairs of numbers as two separate arrays:
In[6]:= a1 = RandomInteger[{1, 100}, 10^6];
a2 = RandomInteger[{1, 100}, 10^6];
In[7]:= First[Timing[a1*a2;]]
Out[7]= 0.012661
Wow, quite a bit faster! Even if the input wasn't provided as two separate arrays (or you have more than two elements in each pair,) we can still do something optimal:
In[8]:= First[Timing[Times##Transpose[array];]]
Out[8]= 0.020391
The moral of this epic is not that ForEach isn't a valuable construct in general, or even in Mathematica, but that you can often obtain the same results more efficiently and more elegantly when you work in a functional mindset, rather than a structural one.
Newer versions of Mathematica (6.0+) have generalized versions of Do[] and Table[] that do almost precisely what you want, by taking an alternate form of iterator argument. For instance,
Do[
Print[i],
{i, {1, 2, 3}}]
is exactly like your
ForEach[i_, {1, 2, 3,},
Print[i]]
Alterntatively, if you really like the specific ForEach syntax, you can make a HoldAll function that implements it, like so:
Attributes[ForEach] = {HoldAll};
ForEach[var_Symbol, list_, expr_] :=
ReleaseHold[
Hold[
Scan[
Block[{var = #},
expr] &,
list]]];
ForEach[vars : {__Symbol}, list_, expr_] :=
ReleaseHold[
Hold[
Scan[
Block[vars,
vars = #;
expr] &,
list]]];
This uses symbols as variable names, not patterns, but that's how the various built-in control structures like Do[] and For[] work.
HoldAll[] functions allow you to put together a pretty wide variety of custom control structures. ReleaseHold[Hold[...]] is usually the easiest way to assemble a bunch of Mathematica code to be evaluated later, and Block[{x = #}, ...]& allows variables in your expression body to be bound to whatever values you want.
In response to dreeves' question below, you can modify this approach to allow for more arbitrary destructuring using the DownValues of a unique symbol.
ForEach[patt_, list_, expr_] :=
ReleaseHold[Hold[
Module[{f},
f[patt] := expr;
Scan[f, list]]]]
At this point, though, I think you may be better off building something on top of Cases.
ForEach[patt_, list_, expr_] :=
With[{bound = list},
ReleaseHold[Hold[
Cases[bound,
patt :> expr];
Null]]]
I like making Null explicit when I'm suppressing the return value of a function. EDIT: I fixed the bug pointed out be dreeves below; I always like using With to interpolate evaluated expressions into Hold* forms.
The built-in Scan basically does this, though it's uglier:
Scan[Print[#]&, {1,2,3}]
It's especially ugly when you want to destructure the elements:
Scan[Print[#[[1]] * #[[2]]]&, {{1,10}, {2,20}, {3,30}}]
The following function avoids the ugliness by converting pattern to body for each element of list.
SetAttributes[ForEach, HoldAll];
ForEach[pat_, lst_, bod_] := Scan[Replace[#, pat:>bod]&, Evaluate#lst]
which can be used as in the example in the question.
PS: The accepted answer induced me to switch to this, which is what I've been using ever since and it seems to work great (except for the caveat I appended to the question):
SetAttributes[ForEach, HoldAll]; (* ForEach[pattern, list, body] *)
ForEach[pat_, lst_, bod_] := ReleaseHold[ (* converts pattern to body for *)
Hold[Cases[Evaluate#lst, pat:>bod];]]; (* each element of list. *)
The built-in Map function does exactly what you want. It can be used in long form:
Map[Print, {1,2,3}]
or short-hand
Print /# {1,2,3}
In your second case, you'd use "Print[Times###]&/#{{1,10}, {2,20}, {3,30}}"
I'd recommend reading the Mathematica help on Map, MapThread, Apply, and Function. They can take bit of getting used to, but once you are, you'll never want to go back!
Here is a slight improvement based on the last answer of dreeves that allows to specify the pattern without Blank (making the syntax similar to other functions like Table or Do) and that uses the level argument of Cases
SetAttributes[ForEach,HoldAll];
ForEach[patt_/; FreeQ[patt, Pattern],list_,expr_,level_:1] :=
Module[{pattWithBlanks,pattern},
pattWithBlanks = patt/.(x_Symbol/;!MemberQ[{"System`"},Context[x]] :> pattern[x,Blank[]]);
pattWithBlanks = pattWithBlanks/.pattern->Pattern;
Cases[Unevaluated#list, pattWithBlanks :> expr, {level}];
Null
];
Tests:
ForEach[{i, j}, {{1, 10}, {2, 20}, {3, 30}}, Print[i*j]]
ForEach[i, {{1, 10}, {2, 20}, {3, 30}}, Print[i], 2]
Mathematica have map functions, so lets say you have a function Functaking one argument. Then just write
Func /# list
Print /# {1, 2, 3, 4, 5}
The return value is a list of the function applied to each element in the in-list.
PrimeQ /# {10, 2, 123, 555}
will return {False,True,False,False}
Thanks to Pillsy and Leonid Shifrin, here's what I'm now using:
SetAttributes[each, HoldAll]; (* each[pattern, list, body] *)
each[pat_, lst_List, bod_] := (* converts pattern to body for *)
(Cases[Unevaluated#lst, pat:>bod]; Null); (* each element of list. *)
each[p_, l_, b_] := (Cases[l, p:>b]; Null); (* (Break/Continue not supported) *)

Resources