How to resolve variable increment issue in a recursive function - scala

Trying to create a pascals triangle using recursive function. Returning value is always zero.
I'm new to Scala programming and not sure if the declaring the value variable in the code is the right way. Appreciate any help with the right approach. Thanks
object Main {
def main(args: Array[String]) {
println("Pascal's Triangle")
for (row <- 0 to 10) {
for (col <- 0 to row)
print(pascal(col, row) + " ")
println()
}
}
var value: Int = 0
def pascal(c: Int, r: Int): Int = {
if (c ==0) 1
else if (c == r ) 1
else
for (col <- c-1 to c) {
value += pascal(col, r - 1)
}
value
}
}
Actual Result
Pascal's Triangle
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
Expected Result
Pascal's Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1

Scala style tries to avoid mutable data (i.e. var).
def pascalTriangle(height :Int) :Unit =
Iterator.iterate(Vector(1))(v => (0+:v:+0).sliding(2).map(_.sum).toVector)
.take(height) //take only as many as needed
.map(_.mkString(" ")) //turn Vector into space separated String
.foreach(println) //force the calculations
Here we create an infinite collection of Vectors, each one longer than the previous. Each Vector is processed to create the required sums, but none of that happens until the foreach() because an Iterator is lazy.

You should avoid using var. In this case, you don't need it. Avoid var, use val. Such is good functional programming practice.
object Pascal {
def main(args: Array[String]) {
println("Pascal's Triangle")
for (row <- 0 to 10) {
for (col <- 0 to row)
print(pascal(col, row) + " ")
println()
}
}
def pascal(c: Int, r: Int): Int = {
if (c ==0) 1
else if (c == r ) 1
else {
val intermediate = for (col <- c - 1 to c) yield pascal(col, r - 1)
intermediate.sum
}
}
}

Related

permutation/combination with specific condition

Let us we have binary number to fill out 9 spots with specific condition: 0 always comes before 1. the possible conditions is 10:
1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1
0 0 0 0 1 1 1 1 1
0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0
Now lest us extent it to 0, 1, 2 with same rule. 0 should be always before 1 and/or 2. 1 should be before 1. Again, 9 spots are available to fill out.
I know that this yields to 55 combinations.
Question:
(1) what is the mathematical formulation to generalize this?
(2) How can I store all those 55 combinations? [any matlab code?]
Thanks
As the commenter said, the answer comes down to stars and bars. You can also think of this as counting the number of non-decreasing sequences i_1 <= i_2 <= ... <= i_k, where k is the number of symbols available and each i_j is a number between 0 and 9.
That said, here's a matlab script that generates all possibilities. Each row of the output matrix is one possible string of digits.
function M = bin_combs(L,k)
% L: length
% k: number of symbols
if k == 1
M = zeros(1,L);
else
M = zeros(0,L);
N = bin_combs(L,k-1);
for i = 1:size(N,1)
row = N(i,:);
for j=find(row==k-2)
new_row = row;
new_row(j:end) = new_row(j:end) + 1;
M = [M;new_row];
end
M = [M;row];
end
end
Some sample output:
>> size(bin_combs(9,3))
ans =
55 9
>> size(bin_combs(9,4))
ans =
220 9

Changing data diagonally in matrix based on formula and table

Let says I have this matrix:
(x)
X = [1 1 1 0 1 0 0 0;
1 1 0 1 0 1 0 0;
1 0 1 1 0 0 1 0;
0 1 1 1 0 0 0 1;
1 0 0 0 1 1 1 0;
0 1 0 0 1 1 0 1;
0 0 1 0 1 0 1 1;
0 0 0 1 0 1 1 1];
and this table:
kzz
_______
1 80
2 80
3 23
4 14
5 63
6 36
7 41
8 5
and this equation:
f = (1/visc)*((2*kzz2*kzz1*az2*az1)/(kzz2*az2*delz+kzz1*az1*delz)
visc = 2
az2 = 6400
az1 = 6400
delz = 30
kzz1 = ? < From the table
kzz2 = ? < From the table
f = (1/2)*((2*kzz2*kzz1*6400*6400)/(kzz2*6400*30+kzz1*6400*30)
this equation represent the diagonal started from column 5 in the matrix, just below the (X).
The required task is: changing the ones in this diagonal to this equation:
f = (1/2)*((2*kzz2*kzz1*6400*6400)/(kzz2*6400*30+kzz1*6400*30)
For the first value in the diagonal " Matrix(5,1) "
kzz2 = 63 and kzz1 = 80 << which obtained from the table
therefore, the equation will be as follow:
f = (1/2)*((2*63*80*6400*6400)/(63*6400*30+80*6400*30)
so based on the location on matrix, the code should take the value from the table and substitute it in the equation.
My trial is:
k = (4);
d = diag(Matrix,k);
n = d;
n(n==1) = f;
XX = XX - diag(d,k) + diag(n,k);
Example of required output
the output should look like this (imaginary numbers)
1 1 1 0 5 0 0 0
1 1 0 1 0 8 0 0
1 0 1 1 0 0 9 0
0 1 1 1 0 0 0 2
1 0 0 0 1 1 1 0
0 1 0 0 1 1 0 1
0 0 1 0 1 0 1 1
0 0 0 1 0 1 1 1
other equations will be used to change the other diagonals to get this result:
1 2 1 0 5 0 0 0
1 1 0 1 0 8 0 0
1 0 1 6 0 0 9 0
0 1 1 1 0 0 0 2
1 0 0 0 1 7 1 0
0 1 0 0 1 1 0 1
0 0 1 0 1 0 1 4
0 0 0 1 0 1 1 1
Your question is not very clear. If the az1,az2,delz,visc are constants and kzz1=kzz(rowIndex), kzz2=kzz(columnIndex) as it seems to be the case from your example, this seems to be ok for what you are trying to do.
az1 = 6400;
az2 = 6400;
delz = 30;
visc = 2;
i=0;
for j=5:8
i=i+1;
X(i,j) = (1/visc)*(2*kzz(j)*kzz(i)*az2*az1)/(kzz(j)*az2*delz+kzz(i)*az1*delz);
end
If you want to generalize for a diagonal starting at the startColumnIndex, the for loop can be something like that:
i=0;
startColumnIndex = 5;
numberOfColumns = size(X,2);
for j = startColumnIndex : numberOfColumns
i=i+1;
.....stuff you want to do......
end

Scala Breeze expansion matrix

In Octave/Matlab I can expand an identity matrix as follows.
>> I = eye(3)
I =
Diagonal Matrix
1 0 0
0 1 0
0 0 1
>> A = [ 3 2 3 2 2 1 3 2 2 1 ]
>> E = I(:, A)
E =
0 0 0 0 0 1 0 0 0 1
0 1 0 1 1 0 0 1 1 0
1 0 1 0 0 0 1 0 0 0
How can I achieve the same thing (i.e. obtain E from A, possibly using I) with Scala/Breeze ?
Got it. Actually very similar to Octave.
scala> val I = DenseMatrix.eye[Int](3)
I: breeze.linalg.DenseMatrix[Int] =
1 0 0
0 1 0
0 0 1
scala> val A = DenseMatrix(2, 1, 2, 1, 1, 0, 2, 1, 1, 0) // zero based in breeze
scala> I(::, A.toArray.toSeq)
res26: breeze.linalg.SliceMatrix[Int,Int,Int] =
0 0 0 0 0 1 0 0 0 1
0 1 0 1 1 0 0 1 1 0
1 0 1 0 0 0 1 0 0 0
The caveats are:
the matrices must contain Integers
indices are 0 based (as opposed to 1 based in Octave/Matlab)

how to Simplify the the boolean function into POS and SOP forms?

I'd like to simplify this boolean function: f(a,b,c,d)=∑(1,3,5,8,9,11,15) to its minimal SOP and POS forms.
My solution is:
SOP: A'·B'·C'·D + A'·B'·C·D + A'·B·C'·D + A·B'·C'·D' + A·B'·C'·D' + A·B'·C·D + A·B·C·D
POS: (A+B+C+D)·(A+B+C'+D')·(A+B'+C+D')·(A'+B+C+D)·(A'+B+C+D')·(A'+B+C'+D')·(A'+B+C'+D')
Is it right?
Is there more to do?
The minimal SOP (sum of products) and the minimal POS (product of sums) of the given boolean function are depicted in these two Karnaugh maps.
Each of the necessary terms corresponds by color with the graphic representation.
Using the Karnaugh map for only four variables is really quick, but I could also use the Quine–McCluskey algorithm or rules of Boolean algebra for the minterms and maxterms in the truth table:
index A B C D output minterms maxterms
-------+---------+--------+-------------+--------------------
0 0 0 0 0 0 ~ M0: (a+b+c+d)
1 0 0 0 1 1 ~ m1: a'·b'·c'·d
2 0 0 1 0 0 ~ M2: (a+b+c'+d)
3 0 0 1 1 1 ~ m3: a'·b'·c·d
4 0 1 0 0 0 ~ M4: (a+b'+c+d)
5 0 1 0 1 1 ~ m5: a'·b·c'·d
6 0 1 1 0 0 ~ M6: (a+b'+c'+d)
7 0 1 1 1 0 ~ M7: (a+b'+c'+d')
8 1 0 0 0 1 ~ m8: a·b'·c'·d'
9 1 0 0 1 1 ~ m9: a·b'·c'·d
10 1 0 1 0 0 ~ M10: (a'+b+c'+d)
11 1 0 1 1 1 ~ m11: a·b'·c·d
12 1 1 0 0 0 ~ M12: (a'+b'+c+d)
13 1 1 0 1 0 ~ M13: (a'+b'+c+d')
14 1 1 1 0 0 ~ M14: (a'+b'+c'+d)
15 1 1 1 1 1 ~ m15: a·b·c·d
You can also check the correctness of your future solutions by using wolfram alpha.

Matrix Row manipulation in Breeze using Scala?

I've a DenseMatrix
1 2 3 0 0 0 0 0 0
0 0 0 11 22 33 0 0 0
0 0 0 0 0 0 111 222 333
I want to remove the first row and then a last row with all 0s
0 0 0 11 22 33 0 0 0
0 0 0 0 0 0 111 222 333
0 0 0 0 0 0 0 0 0
How do I achieve this in Breeze ?
First, gather the rows you still want:
val subset = matrix(::, 2 to 3)
then add the zeroes:
val newMatrix = DenseMatrix.horzcat(subset, DenseMatrix.zeros[Double](1,9))
I might have mixed up rows and columns in the last line.