Squaring a list of number in Mathematica - wolfram-mathematica

So for a homework question in my discrete mathematical modeling class we were asked to make a function in Mathematica which inputs a list and squares each element of that list. It's an easy task and I've already done the following:
sqList[list_] = (list)^2;
But I want to do this with a "for loop". Here's my code:
sqList2[list2_] :=
(
For[j = 1, j <= Length[list2], j++,
(
list2[j]^2;
)];
list2
);
I'm more used to Matlab and am trying to move up the learning curve. This code makes sense to me and will really appreciate any directions in which I should take this. I've tried debugging this code, but like I said I'm not used to this program nor to its stack track. I couldn't find any problems, but it outputs the exact list I input.

Using an example list
list = {1, 2, 3};
sqList[list_] := list^2
sqList[list]
{1, 4, 9}
This works because Power (^) is listable:
Attributes[Power]
{Listable, NumericFunction, OneIdentity, Protected}
To process the list element by element
sqList2[list2_] := Module[{squaredlist = {}, j},
For[j = 1, j <= Length[list2], j++,
AppendTo[squaredlist, list2[[j]]^2]];
squaredlist]
sqList2[list]
{1, 4, 9}
Alternatively Map could be used
sqList3[list3_List] := Map[#^2 &, list3]
sqList3[list]
{1, 4, 9}
Note
This form of function, close to the one you tried, would not have worked because list2 is an input variable and cannot be modified.
sqList2[list2_] := (
For[j = 1, j <= Length[list2], j++,
list2[[j]] = list2[[j]]^2];
list2)
The minimal modification to make it work would be:-
sqList2[list2input_] := (
list2 = list2input;
For[j = 1, j <= Length[list2], j++,
list2[[j]] = list2[[j]]^2];
list2)
Also, For loops do not output running results; that's why the output needs to be collected inside the loop.

Related

Create variable names in loop

My aim is to create a lot of functions f_i in a loop. These functions depend on parameters a[[i]], which can be taken from array A = {a1, a2, ...}. In order to eliminate the influence of the interator i, which leads to the situation when all functions are the same, I aspire to create variable names for each iteration.
The example: suppose I have got the array W = {1,2,3, ..., 100} and I should create variables w1 = 1, w2 = 2, ..., w100 = 100. I am trying to do this with the help of a for-loop:
loc[expr1_, expr2_] :=
ToExpression[StringJoin[ToString[expr1], ToString[expr2]]];
For[i = 1, i <= 100, i++,
{
loc[w, i] = W[[i]];
}]
When I need to see which value variable wk contains, then wk is not defined. But loc[w, k] = k is known.
How can I define variables wi? Or is there another way to create functions in a loop?
Thanks in advance
The way you are using {} leads me to believe that you have prior experience with other programming languages.
Mathematica is a very different language and some of what you know and expect will be wrong. Mathematica only uses {} to mean that is a list of elements. It is not used to group blocks of code. ; is more often used to group blocks of code.
Next, try
W={1,2,3};
For[i=i,i<=3,i++,
ToExpression["w"<>ToString[i]<>"="<>ToString[i]]
];
w2
and see that that returns
2
I understand that there is an intense desire in people who have been trained in other programming languages to use For to accomplish things. There are other ways o doing that for most purposes in Mathematica.
For one simple example
W={1,2,3};
Map[ToExpression["z"<>ToString[#]<>"="<>ToString[#]]&,W];
z2
returns
2
where I used z instead of w just to be certain that it wasn't showing me a prior cached value of w2
You can even do things like
W={1,2,3};
loc[n_,v_]:=ToExpression[ToString[n]<>ToString[v]<>"="<>ToString[v]];
Map[loc[a,#]&,W];
a3
which returns
3
Ordinarily, you will use indexed variables for this. E.g.,
ClearAll[x, xs]
n = 4
xs = Array[Indexed[x, #] &, 4]
Example use with random data:
RandomSeed[314]
mA = RandomInteger[{0, 99}, {n, n}]
vb = RandomInteger[{0, 99}, n]
Solve[mA.xs == vb, xs]
This is just for illustration; one would ordinarily use LinearSolve for the example problem. E.g., MapThread[Rule, {xs, LinearSolve[mA, vb]}].
It would be simpler to use a function variable, e.g. w[1], but here is a method to define w1 etc.
Note Clear can clear assignments using string versions of the symbols.
W = {1, 2, 7, 9};
Clear ## Map["w" <> ToString[#] &, W]
Map[(Evaluate[Symbol["w" <> ToString[#]]] = #) &, W];
w9
9
Symbol /# Map["w" <> ToString[#] &, W]
{1, 2, 7, 9}
Alternatively, with a function variable . . .
Map[(w[#] = #) &, W]
{1, 2, 7, 9}
w[9]
9
Also, using the OP's structure
Clear[loc]
Clear[w]
Clear ## Map["w" <> ToString[#] &, W]
W = {1, 2, 3, 4};
loc[expr1_, expr2_] := StringJoin[ToString[expr1], ToString[expr2]]
For[i = 1, i <= 4, i++, Evaluate[Symbol[loc[w, i]]] = W[[i]]]
Symbol /# Map["w" <> ToString[#] &, W]
{1, 2, 3, 4}
Note Evaluate[Symbol[loc[w, i]]] = W[[i]]] has the advantage that if the data at W[[i]] is a string it does not get transformed as it would by using ToExpression.

Naming Multiple Variables In A Loop

first time asking a question on here. I am trying to make multiple variables in Mathematica which all have the same name except for the number after it. For example, if the list I am using has 7 members, it will make the variables: member1, member2, member3, member4, and so on. If this is unclear I left an example below of how I wished it would work. Hopefully it is an easy solution!
list = {1,2,3,4,5}
For[i = 1, i < 6, i++,
member[i] = list[i]
]
Try this
list = {1, 2, 3, 4, 5};
For[i = 1, i < 6, i++,
ToExpression["member"<>ToString[i]<>"="<>ToString[list[[i]]]]]
followed by
?member3
which should show you that member3 has been assigned a value of 3
This is creating a string and then evaluating it as a line of code.
You can use Symbol with Evaluate.
list = {1, 2, 3, 4, 5};
For[i = 1, i < 6, i++,
Evaluate[Symbol["member" <> ToString[i]]] = list[[i]]
]
member3
3
Alternatively, using Array instead of For.
Array[(Evaluate[Symbol["member" <> ToString[#]]] = list[[#]]) &, Length[list]]
And a version that clears pre-existing values.
Array[(With[{x = "member" <> ToString[#]},
Clear[x];
Evaluate[Symbol[x]] = list[[#]]]) &, Length[list]]

Reducing length of list with Total and a threshold parameter

I'm looking for a way to reduce the length of a huge list with the Total function and a threshold parameter. I would like to avoid the use of For and If (coming from old habits).
Example :
List that I want to "reduce" :{1,5,3,8,11,3,4} with a threshold of 5.
Output that I want : {6,11,11,7}
That means that I use the Total function on the first parts of the list and look if the result of this function is higher than my threshold. If so, I use the result of the Total function and go to the next part of the list.
Another example is {1,1,1,1,1} with a threshold of 5. Result should be {5}.
Thanks!
EDIT : it is working but it is pretty slow. Any ideas in order to be faster?
EDIT 2 : the loop stuff (quit simple and not smart)
For[i = 1, i < Length[mylist] + 1, i++,
sum = sum + mylist[[i]];
If[sum > Threshold ,
result = Append[result , sum]; sum = 0; ]; ];
EDIT 3 : I have now a new thing to do.
I have to work now with a 2D list like {{1,2}{4,9}{1,3}{0,5}{7,3}}
It is more or less the same idea but the 1st and 2nd part of the list have to be higher than the thresold stuff (both of them).
Example : If lst[[1]] and lst[[2]] > threshold do the summuation for each part of the 2D list. I tried to adapt the f2 function from Mr.Wizard for this case but I didn't succeed. If it is easier, I can provide 2 independant lists and work with this input f3[lst1_,lst2_,thres_]:=
Reap[Sow#Fold[If[Element of the lst1 > thr && Element of the lst2, Sow##; #2, # + #2] &, 0, lst1]][[2, 1]] for example.
EDIT 4 :
You are right, it is not really clear. But the use of the Min## > thr statement is working perfectly.
Old code (ugly and not smart at all):
sumP = 0;
resP = {};
sumU = 0;
resU = {};
For[i = 1, i < Length[list1 + 1, i++,
sumP = sumP + list1[[i]];
sumU = sumU + list2[[i]];
If[sumP > 5 && sumU > 5 ,
resP = Append[resP, sumP]; sumP = 0;
resU = Append[resU, sumU]; sumU = 0;
];
]
NEW fast by Mr.Wizard :
f6[lst_, thr_] :=
Reap[Sow#Fold[If[Min## > thr , Sow##1; #2, #1 + #2] &, 0, lst]][[2,
1]]
That ~40times faster. Thanks a lot.
Thread[{resP, resU}] == f6[Thread[{list1,list2}], 5] True
I recommend using Fold for this kind of operation, combined with either linked lists or Sow and Reap to accumulate results. Append is slow because lists in Mathematica are arrays and must be reallocated every time an element is appended.
Starting with:
lst = {2, 6, 4, 4, 1, 3, 1, 2, 4, 1, 2, 4, 0, 7, 4};
Here is the linked-list version:
Flatten # Fold[If[Last## > 5, {#, #2}, {First##, Last## + #2}] &, {{}, 0}, lst]
{8, 8, 7, 7, 11, 4}
This is what the output looks like before Flatten:
{{{{{{{}, 8}, 8}, 7}, 7}, 11}, 4}
Here is the method using Sow and Reap:
Reap[Sow # Fold[If[# > 5, Sow##; #2, # + #2] &, 0, lst]][[2, 1]]
{8, 8, 7, 7, 11, 4}
A similar method applied to other problems: (1) (2)
The Sow # on the outside of Fold effectively appends the last element of the sequence which would otherwise be dropped by the algorithm.
Here are the methods packaged as functions, along with george's for easy comparison:
f1[lst_, thr_] :=
Flatten # Fold[If[Last## > thr, {#, #2}, {First##, Last## + #2}] &, {{}, 0}, lst]
f2[lst_, thr_] :=
Reap[Sow#Fold[If[# > thr, Sow##; #2, # + #2] &, 0, lst]][[2, 1]]
george[t_, thresh_] := Module[{i = 0, s},
Reap[While[i < Length[t], s = 0;
While[++i <= Length[t] && (s += t[[i]]) < thresh]; Sow[s]]][[2, 1]]
]
Timings:
big = RandomInteger[9, 500000];
george[big, 5] // Timing // First
1.279
f1[big, 5] // Timing // First
f2[big, 5] // Timing // First
0.593
0.468
Here is the obvious approach which is oh 300x faster.. Pretty isn't always best.
t = Random[Integer, 10] & /# Range[2000];
threshold = 4;
Timing[
i = 0;
t0 = Reap[
While[i < Length[t], s = 0;
While[++i <= Length[t] && (s += t[[i]]) < threshold ];
Sow[s]]][[2, 1]]][[1]]
Total[t] == Total[t0]
Timing[ t1 =
t //. {a___, b_ /; b < threshold, c_, d___} -> {a, b + c, d} ][[1]]
t1 == t0
I interpret your requirement as:
if an element in the list is less than the threshold value, add it to the next element in the list;
repeat this process until the list no longer changes.
So, for the threshold 5 and the input list {1,5,3,8,11,3,4} you'ld get
{6,3,8,11,3,4}
{6,11,11,3,4}
{6,11,11,7}
EDIT
I've now tested this solution to your problem ...
Implement the operation by using a replacement rule:
myList = {1,5,3,8,11,3,4}
threshold = 5
mylist = mylist //. {a___, b_ /; b < threshold, c_, d___} :> {a, b+c, d}
Note the use of ReplaceRepeated (symbolification //.).

Matching a Finite Range of Elements with Patterns

While working on my answer to this question, it occurred to me that it is difficult to match a finite range of elements. With the built in patterns, you can match 1 element (_), 1 or more elements (__), or zero or more elements (___). To match more than one element, I used PatternSequence, like this
a:PatternSequence[_,_,_]
or, more generically
a:PatternSequence##Array[_&,3].
(Using a Condition would have also worked.) To match a range of n to m elements we could do
a:Alternatives##( PatternSequence ### Array[_&, {n,m}] ),
but that is a rather convoluted way to accomplish something that can be done by
a__ /; n <= Length[{a}] <= m.
However, this brings up an interesting question, using the Condition form it is straightforward to match the range 0 to n,
a___ /; Length[{a}] <= n,
but can this be done using patterns alone, i.e. without using Condition (/;)? More specifically, how would one go about matching 0 elements without adding a condition? Also, which is faster?
Maybe you could do something with Repeated. E.g.
Cases[{{1, 2, 3}, {1}, {1, 2, 3, 4, 5}, {1,2}}, {Repeated[_, {2, 4}]}]
gives the same result as
Cases[{{1, 2, 3}, {1}, {1, 2, 3, 4, 5}, {1,2}}, {a___ /; 2 <= Length[{a}] <= 4}]
The first method seems faster than the second. For example
tab = Table[Range[RandomInteger[1000]], {1000}];
Timing[t1 = Cases[tab, {a___ /; 0 <= Length[{a}] <= 100}];]
Timing[t2 = Cases[tab, {Repeated[_, {0, 100}]}];]
SameQ[t1, t2]
returns on my system
{0.027801, Null}
{0.000733, Null}
True

List all k-tuples with entries summing to n, ignoring rotations

Is there an efficient algorithm for finding all sequences of k non-negative integers that sum to n, while avoiding rotations (completely, if possible)? The order matters, but rotations are redundant for the problem I'm working on.
For example, with k = 3 and n = 3, I would want to get a list like the following:
(3, 0, 0), (2, 1, 0), (2, 0, 1), (1, 1, 1).
The tuple (0, 3, 0) should not be on the list, since it is a rotation of (3, 0, 0). However, (0, 3, 0) could be in the list instead of (3, 0, 0). Note that both (2, 1, 0) and (2, 0, 1) are on the list -- I do not want to avoid all permutations of a tuple, just rotations. Additionally, 0 is a valid entry -- I am not looking for partitions of n.
My current procedure is to loop from over 1 <= i <= n, set the first entry equal to i, and then recursively solve the problem for n' = n - i and k' = k - 1. I get some speed-up by mandating that no entry is strictly greater than the first, but this approach still generate a lot of rotations -- for example, given n = 4 and k = 3, both (2,2,0) and (2,0,2) are in the output list.
Edit: Added clarifications in bold. I apologize for not making these issues as clear as I should have in the original post.
You can first generate the partitions (which ignore order completely) as a tuple (x_1, x_2, ..., x_n)
where x_i = number of times i occurs.
So Sum i* x_i = n.
I believe you already know how to do this (from your comments).
Once you have a partition, you can now generate the permutations for this (viewing it as a multiset {1,1,...,2,2...,...n}, where i occurs x_i times) which ignore rotations, using the answer to this question:
Is there an algorithm to generate all unique circular permutations of a multiset?
Hope that helps.
You could just sort your solutions and eliminate rotations that way.
OR
you can try to make your recursive solution build tuples that will only ever be sorted
how? here's something I made up quickly
static list<tuple> tups;
void recurse(tuple l, int n, int k, int m)
{
if (k == 0 && n == 0)
{
tups.add(l);
return;
}
if (k == 0)
return;
if (k*m > n) //prunes out tuples that could not possibly be sorted
return;
else
for(int x = m; x <= n; x++)
recurse(l.add(x), n-x, k-1, x); //try only tuples that are increasing
}
call this with m = 0 and an empty list for the initial step.
here's a C# console app implementation : http://freetexthost.com/b0i05jkb4e
Oh, I see my mistake in the assumption of rotation, I thought you just meant permutations, not an actual rotation.
However, you can extend my solution to create non-rotational permutations of the unique increasing tuples. I'm working on it now
You need to generate Integer Partitions in lexicographical order.
Here is a very good paper with fast algorithms.
HTH.
Note that CAS programs usually implement these functions. For example in Mathematica:
Innput: IntegerPartitions[10, {3}]
Output: {{8, 1, 1}, {7, 2, 1}, {6, 3, 1},
{6, 2, 2}, {5, 4, 1}, {5, 3, 2},
{4, 4, 2}, {4, 3, 3}}

Resources