R apply-like function for updating 2 arrays simultaneously? - arrays

I am new to R, and looking for an apply type function to work with 2 arrays at once (simultaneous update).
For example, let's say I have some variables X and P:
X = array(rep(0, 10), dim=c(10, 1))
P = array(rep(1, 10), dim=c(10, 1))
which are governed by the system of equations:
X[k,] = 2*X[k-1]
P[k,] = 3*X[k] + X[k-1] + 3
Obviously, this can easily be accomplished with a for-loop, however, I have read/confirmed myself that for loops work horrendously for large inputs, and I wanted to start getting into good R coding practice, so I am wondering, what is the best way to do this in an apply-type logic? I am looking for something like,
sapply(2:dim(X)[1], function(k) {
X[k,] = 2*X[k-1]
P[k,] = 3*X[k] + X[k-1] + 3
})
But this obviously won't work, as it doesn't actually update X and P internally. Any tips/tricks for how to make my for-loops faster, and get in better R coding practice? Thanks in advance!

You could do the following below. The <<- operator will set X and P outside of the function
sapply(2:dim(X)[1], function(k) {
X[k,] <<- 2*X[k-1]
P[k,] <<- 3*X[k] + X[k-1] + 3
})
As pointed out by thelatemail in the comments, using <<- can be problematic because of the side effects it can have. See the links below, the one comparing for loops (and other loops) to the apply family of functions.
Here is a link to documentation on assignment operators in R.
Here is a StackOverflow link on for loop vs. apply functions that talks about performance.

Related

Understanding a key concept in Minizinc - declaring set of variable, then using it to declare an array of values

I'm working through the Introduction to Minizink course on Coursera and have come to an example which I really would like to have a better intuitive understanding of.
The model in this vieo https://www.coursera.org/learn/basic-modeling/lecture/PZO1B/1-1-7-global-constraints declares the following parameters
set of int: DIGIT = 1..9;
array[DIGIT] of int: rods = [1,2,3,4,5,2,3,4,5];
with these constraints after
constraint rods[M1] + rods[M2] + rods[M3] +
rods[M4] + rods[M5] = 12;
constraint 2303 + M1 * 10 + 980 + M2 * 1000 + M3
= 301 + M4 * 1000 + M5 * 10;
I'm struggling to visualise what's going on here. To check, a set called DIGIT is declared consisting of numbers 1 to 9, which could also be viewed as {1,...,9}, then it's 'also' being 'instantiated/declared as/associated with' an array that looks like as it is written above inside the [], matched up by the corresponding index. In contrast, declaring var 1..9: DIGIT would create a range of values that DIGIT could take, which isn't an array or set.
In fact, as I write this, I actually remembered how I understood earlier examples in the course, but I'll finish this pose to perhaps get further perspectives and clarity on it, or see if I'm still missing something.
Say for example you have an enumerated set FRUIT = {apple,orange,banana} with which you could associate an array of integers using for example array[FRUIT] of int: whatever = [4,9834,-42]. Then, use either the enumareted possibilities by declaring var FRUIT: Opt1;, so Opt1 can take the value of either apple, orange or banana, depending on the type of constraint used, while using whatever[Opt1] inside a constraint would take on the value of either 4, 9834 or -42, depending on the fruit currently being 'tested' as Opt1?
So as is often the case, explaining something to someone actually helps you to understand it better yourself, so I think I get it, but I wonder if there is anything off or missing about how I interpret the way this aspect of Minizinc works?

Best way to find all unique combinations of elements of an array?

I have an array of some particular ints, and I want to find all the unique combinations (by addition) of these ints. I'm sure there's a way to do this functionally; I'm trying to avoid the iterative way of shoving for loops inside of for loops. I'm using Rust in this case, but this question is more generally a, "functional programming, how do?" take.
My first thought is that I should just zip every entry in v with every other entry, reduce them by addition to a single element, and filter duplicates. This is O(|v|^2), which feels bad, meaning I'm probably missing something fairly obvious to the functional whiz kids. Also I'm not even sure how to do it, I'd probably use a for loop to construct my new massive array.
My first pass: note that v holds all the numbers I care about.
let mut massive_arr = Vec::new();
for &elem in v.iter(){
for &elem2 in v.iter(){
massive_arr.push((elem,elem2));
}
}
let mut single_massive = Vec::new();
for &tuple in massive_arr.iter(){
single_massive.push(tuple.0 + tuple.1);
}
single_massive.dedup();
let summand: usize = single_massive.iter().sum(); println!("The sum of all that junk is {:?}", summand);```
Help me baptize my depraved iterations in the pure light of functional programming.
edited: I threw up an example before as I was still figuring out the implementation that actually worked, and the question was more of a, how do I do this better question. The thing above now actually works (but is still ugly!).
You can possibly use itertools (I do not have a compiler at hand, but you probably get the idea):
use itertools::Itertools;
iproduct!(v.iter(), v.iter()) // construct all pairs
.map(|tuple| tuple.0+tuple.1) // sum each pair
.unique() // de-duplicate (uses a HashMap internally)
.sum() // sum up
All this is still O(n^2) which is -- as far as I see -- asymptotically optimal, because all pairs of numbers might be needed.
To avoid the obvious duplicates, you can use tuple_combinations:
v.iter()
.tuple_combinations()
.map(|(a, b)| a+b)
.unique()
.sum()
Improving on #phimuemue's answer, you can avoid the obvious duplicates like this:
v.iter()
.enumerate()
.flat_map (|(i, a)| v[i+1..].iter().map (move |b| a+b))
.unique() // May not be needed or what you really want, see note below
.sum()
Playground
Note however that this may not give you the answer you really want if multiple pairs of numbers have the same sum. For example, given vec![1, 2, 3, 4] as input, which do you expect:
(1+2) + (1+3) + (1+4) + (2+3) + (2+4) + (3+4) = 30
or 3 + 4 + 5 + 6 + 7 = 25 because 1+4 == 2+3 == 5 is only counted once?

Excluding certain conditioned variable in calculation using nested ifelse statement

I have a formula, says :
v12=-(ln(v6)*v6+ln(v7)*v7+ln(v8)*v8+ln(v9)*v9).
I have 0 number in one or two variables in the calculation. Since ln(0) is undefined, the calculation was not performed. Is there any way to ignore any variables that contain 0 in the calculation and proceed without it? I tried using na, but it failed.
Two choices (one is in R language which is what you originally asked for :S ):
1) What you literally asked for, in R, by using vectorization:
vec = c(v6,v7,v8,v9)
result = -sum(ifelse(vec!=0, vec*log(vec), 0))
2) log1p() is the safer and numerically more well-behaved alternative for small numbers:
v12 = -sum(lapply(c(v6,v7,v8,v9), function(x) { x*log1p(x) } ))
You can use log1p in C too!

Dynamically creating and naming an array

Consider the following code snippet
for i = 1:100
Yi= x(i:i + 3); % i in Yi is not an index but subscript,
% x is some array having sufficient values
i = i + 3
end
Basically I want that each time the for loop runs the subscript changes from 1 to 2, 3, ..., 100. SO in effect after 100 iterations I will be having 100 arrays, starting with Y1 to Y100.
What could be the simplest way to implement this in MATLAB?
UPDATE
This is to be run 15 times
Y1 = 64;
fft_x = 2 * abs(Y1(5));
For simplicity I have taken constant inputs.
Now I am trying to use cell based on Marc's answer:
Y1 = cell(15,1);
fft_x = cell(15,1);
for i = 1:15
Y1{i,1} = 64;
fft_x{i,1} = 2 * abs(Y1(5));
end
I think I need to do some changes in abs(). Please suggest.
It is impossible to make variably-named variables in matlab. The common solution is to use a cell array for Y:
Y=cell(100,1);
for i =1:100
Y{i,1}= x(i:i+3);
i=i+3;
end
Note that the line i=i+3 inside the for-loop has no effect. You can just remove it.
Y=cell(100,1);
for i =1:100
Y{i,1}= x(i:i+3);
end
It is possible to make variably-named variables in matlab. If you really want this do something like this:
for i = 1:4:100
eval(['Y', num2str((i+3)/4), '=x(i:i+3);']);
end
How you organize your indexing depends on what you plan to do with x of course...
Yes, you can dynamically name variables. However, it's almost never a good idea and there are much better/safer/faster alternatives, e.g. cell arrays as demonstrated by #Marc Claesen.
Look at the assignin function (and the related eval). You could do what asked for with:
for i = 1:100
assignin('caller',['Y' int2str(i)],rand(1,i))
end
Another related function is genvarname. Don't use these unless you really need them.

Solving Kakuro puzzles

Here's a good one to reflect on:
http://en.wikipedia.org/wiki/Kakuro
I'm attempting to make a solver for this game. The paperwork is done (reading an initial file with a variable number of columns and rows. It's assumed the input file follows the rules of the game so the game is always solvable. Take your time to read the game rules.
I've taken care of the data structure which I think will suit best:
struct aSquare { int verticalSum; int horizontalSum; int value; }
And made an "array" of these dynamically to work on.
I made it so that the black squares have value of -1 and white squares (the actual solution squares) initialize at 0. You can also get the position of each aSquare struct from the array easily, no need to make additional struct fields for it.
Now the algorithm ... How in the world will I conciliate all these sums and find a general way that will solve all types of grids. I been struggling with this all afternoon to no avail.
Help is appreciated, have fun!
*EDIT: I just realized the actual link I posted has some tips regarding solving techniques. I will still keep this up to see what people come up with.
Regarding Constraint Programming: Here are some different implementations of how to solve a Kakuro puzzle with Constraint Programming (all using the same basic principle). The problem instance is fixed in the program.
Google or-tools/Python: http://www.hakank.org/google_or_tools/kakuro.py
Comet: http://www.hakank.org/comet/kakuro.co
MiniZinc: http://www.hakank.org/minizinc/kakuro.mzn
SICStus: http://www.hakank.org/sicstus/kakuro.pl
ECLiPSe: http://www.hakank.org/eclipse/kakuro.ecl
Gecode: http://www.hakank.org/gecode/kakuro.cpp
Google or-tools/C#: http://hakank.org/google_or_tools/kakuro.cs
Answer Set Programming: http://hakank.org/asp/kakuro.lp
Edit: Added Google or-tools/C# and Answer Set Programming.
A simple brute-force solver for Sudoku takes miliseconds to run, so you don't need to bother implementing any special tactics. I think that in case of Kakuro this will be the same. A simple algorithm:
def solve(kakuro):
if kakuro has no empty fields:
print kakuro
stop.
else:
position = pick a position
values = [calculate possible legal values for that field]
for value in values:
kakuro[position] = value
solve(kakuro)
kakuro[position] = None # unset after trying all possibilities
This algorithm might work better if you find the best order of fields to fill. Try to choose fields which will be the most constrained (as in: there are not many values that are legal).
Anyway, this will be probably the simplest to implement, so try it and look for more sophisticated solvers only if this one will not work. (Actually this is one of the simplest constraint programming solvers; real CP solvers are much more complicated, of course).
If your interest is ultimately in making a software solver for these games, but not getting into the algorithmic details, I recommend using a Constraint Programming (CP) engine. CP is a declarative programming paradigm that is very well suited to these sorts of problems. Several commercial and open source CP engines are available.
http://en.wikipedia.org/wiki/Constraint_programming
I would guess that Linear Programming can be easily used to solve this kind of game.. then this is an integer problem for which exact solutions does exist.. (branch and bound? cutting-plane?)
In any case using a table with the most certain combinations will be useful for sure (eg http://www.enigmoteka.com/Kakuro%20Cheatsheet.pdf)
I have found some nice bit-manipulation tricks that speed up Kakuro solving. You can pick up the source here.
This is straight-forward linear algebra, use vector/matrix manipulation techniques to solve.
[edit - answering the comments]
a + b + 0 + d + 0 = n1
0 + b + c + 0 + e = n2
a + 0 + c + 0 + 0 = n3
a + b + c + 0 + e = n4
a + 0 + c + d + 0 = n5
Above is converted to a matrix, and by adding and subtracting multiples of the rows, you end up with:
a 0 0 0 0 na
0 b 0 0 0 nb
0 0 c 0 0 nc
0 0 0 d 0 nd
0 0 0 0 e ne
No combinatorics, all remain integers.

Resources