var PieceMap2 = {
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"
};
List<List<String>> InitialBoard1 =
List.filled(8, List.filled(8, "a", growable: false), growable: false);
void main() {
for (int i = 8; i > 0; i--) {
for (int j = 0; j <= 7; j++) {
String a = PieceMap2[j] ?? "null";
String b = '$i';
String c = a + b;
print(c);
InitialBoard1[i - 1][j] = c;
}
print(InitialBoard1[i - 1]);
}
for (int i = 0; i < 8; i++) {
print(InitialBoard1[i]);
}
}
This is my program. Every time I'm updating InitialBoard1 but every update of a row, seemingly, overwrites previous updates. When i print it at the end, this is the output i get.
a8
b8
c8
d8
e8
f8
g8
h8
[a8, b8, c8, d8, e8, f8, g8, h8]
a7
b7
c7
d7
e7
f7
g7
h7
[a7, b7, c7, d7, e7, f7, g7, h7]
a6
b6
c6
d6
e6
f6
g6
h6
[a6, b6, c6, d6, e6, f6, g6, h6]
a5
b5
c5
d5
e5
f5
g5
h5
[a5, b5, c5, d5, e5, f5, g5, h5]
a4
b4
c4
d4
e4
f4
g4
h4
[a4, b4, c4, d4, e4, f4, g4, h4]
a3
b3
c3
d3
e3
f3
g3
h3
[a3, b3, c3, d3, e3, f3, g3, h3]
a2
b2
c2
d2
e2
f2
g2
h2
[a2, b2, c2, d2, e2, f2, g2, h2]
a1
b1
c1
d1
e1
f1
g1
h1
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
[a1, b1, c1, d1, e1, f1, g1, h1]
I was expecting to get the final updated value of InitialBoard1 as:
[a1, b1, c1, d1, e1, f1, g1, h1]
.
.
.
[a6, b6, c6, d6, e6, f6, g6, h6]
[a7, b7, c7, d7, e7, f7, g7, h7]
[a8, b8, c8, d8, e8, f8, g8, h8]
Any help as to why i'm not getting the expected result would be appreciated. Thank you.
When you use List.filled it fills the list with the same value multiple times. In your case you end up with 8 references to the same array. When you print each reference at the end, they are of course the same.
Instead use List.generate. This will generate the list by calling the function 8 times, creating 8 different copies.
final InitialBoard1 = List.generate(
8,
(_) => List.filled(8, 'a', growable: false),
);
Try the following code to get your desired result:
Map<int, String> PieceMap2 = {
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"
};
List<List<String>> InitialBoard1 = [];
void main() {
for (int i = 0; i < PieceMap2.length; i++) {
List<String> list = [];
for (int j = 0; j < PieceMap2.length; j++) {
list.add("${PieceMap2[j]}${(i + 1).toString()}");
}
InitialBoard1.add(list);
}
for (int i = 0; i < InitialBoard1.length; i++) {
print(InitialBoard1[i]);
}
}
I wanted to know if there is a efficient way using MATLAB vectorization to generate a specific matrix from two vectors.
Suppose the vectors are
x = [u v]
y = [a1 a2 a3 b1 b2 b3]
where u, v, a1, a2, a3, b1, b2, b3 are some real numbers.
The 2-column matrix that I wish to generate using these vectors is
M = [u a1;
u a2;
u a3;
v a1;
v a2;
v a3;
u b1;
u b2;
u b3;
v b1;
v b2;
v b3]
In general, the length of x can be anything and the length of y is multiple of 3. Here is the code that I have now, but I think there should some better way (that possibly avoids the use of for-loop):
M = [];
Y = reshape(y, 3, []);
for j = 1:size(Y, 2)
[a, b] = meshgrid(x, Y(:, j));
L = [a(:) b(:)];
M = [M; L];
end
A solution using repmat and repelem :
M = [repmat(repelem(x(:),3),numel(y)/3,1) , ...
reshape(repmat(reshape(y,3,[]),numel(x),1),[],1)];
you have a pretty strange order in M. Is that order important? if not, or if you are happy to fix the order later yourself I have two solutions:
1) code
[a,b] = meshgrid(x,y);
M = [a(:) b(:)]
will give you:
M = [
u a1
u a2
u a3
u b1
u b2
u b3
v a1
v a2
v a3
v b1
v b2
v b3]
and
2) code M = combvec(x, y)' gives you:
M = [
u a1
v a1
u a2
v a2
u a3
v a3
u b1
v b1
u b2
v b2
u b3
v b3]
I am working through Andrew Ng new deep learning Coursera course.
We are implementing the following code :
def forward_propagation_with_dropout(X, parameters, keep_prob = 0.5):
np.random.seed(1)
# retrieve parameters
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
W3 = parameters["W3"]
b3 = parameters["b3"]
# LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID
Z1 = np.dot(W1, X) + b1
A1 = relu(Z1)
### START CODE HERE ### (approx. 4 lines) # Steps 1-4 below correspond to the Steps 1-4 described above.
D1 = np.random.rand(*A1.shape) # Step 1: initialize matrix D1 = np.random.rand(..., ...)
D1 = (D1 < 0.5) # Step 2: convert entries of D1 to 0 or 1 (using keep_prob as the threshold)
A1 = A1*D1 # Step 3: shut down some neurons of A1
A1 = A1 / keep_prob # Step 4: scale the value of neurons that haven't been shut down
### END CODE HERE ###
Z2 = np.dot(W2, A1) + b2
A2 = relu(Z2)
### START CODE HERE ### (approx. 4 lines)
D2 =np.random.rand(*A2.shape) # Step 1: initialize matrix D2 = np.random.rand(..., ...)
D2 = (D2 < 0.5) # Step 2: convert entries of D2 to 0 or 1 (using keep_prob as the threshold)
A2 = A2 * D2 # Step 3: shut down some neurons of A2
A2 = A2 / keep_prob # Step 4: scale the value of neurons that haven't been shut down
### END CODE HERE ###
Z3 = np.dot(W3, A2) + b3
A3 = sigmoid(Z3)
cache = (Z1, D1, A1, W1, b1, Z2, D2, A2, W2, b2, Z3, A3, W3, b3)
return A3, cache
Calling:
X_assess, parameters = forward_propagation_with_dropout_test_case()
A3, cache = forward_propagation_with_dropout(X_assess, parameters, keep_prob = 0.7)
print ("A3 = " + str(A3))
My output was :
A3 = [[ 0.36974721 0.49683389 0.04565099 0.49683389 0.36974721]]
The expected output should be :
A3 = [[ 0.36974721 0.00305176 0.04565099 0.49683389 0.36974721]]
Only one number difference. Any ideas why ?
I think it is because of the way I shaped D1 and D2.
I think it is because you put D1 = (D1 < 0.5) and D2 = (D2 < 0.5)
You need to put "keep_prob" instead of 0.5
So I wasn't clear... I thought the question would have been too long. Here goes:
original problem:
ca=3.96;
c0t(1)=2*ca/10;
c1t(1)=ca;
c2t(1)=20/56;
c3t(1)=20/56;
syms c0 c1 c2 c3 c4 c5 c6 c7 c8
eqn1 = c0(1)+c4(1)+c8(1) == c0t(1);
eqn2 = c1(1)+c4(1)+c5(1)+c6(1)+2*c7(1)+2*c8(1) == c1t(1);
eqn3 = c2t(1)==c2(1)+c5(1);
eqn4 = c3t(1)==c3(1)+c6(1)+c7(1)+c8(1);
eqn5 = c4(1)==c1(1)*c0(1)*10^1.98;
eqn6 = c5(1)==c1(1)*c2(1)*10^2.25;
eqn7 = c6(1)==c1(1)*c3(1)*10^4.04;
eqn8 = c7(1)==c1(1)^2*c3(1)*10^5.38;
eqn9 = c8(1)==c1(1)^2*c0(1)*c3(1)*10^8.1;
sol = solve([eqn1, eqn2, eqn3, eqn4, eqn5, eqn6, eqn7, eqn8, eqn9], [c0, c1, c2, c3, c4, c5, c6, c7, c8]);
c0Sol = sol.c0
c1Sol = sol.c1
c2Sol = sol.c2
c3Sol = sol.c3
c4Sol = sol.c4
c5Sol = sol.c5
c6Sol = sol.c6
c7Sol = sol.c7
c8Sol = sol.c8
AND the result is something like this
c0Sol =
(...*RootOf(z^9 - (... + (...*z^7)/... - (...*z^6)/... + (...*z^5)/... - (...*z^4)/... + (...*z^3)/... - (...*z^2)/... + ...................
This has a few pages of digits and operators. On top of this I get 8 more things for this c0Sol and the same for all other cxSol.
What I need is a number like 1.23 * 10 ^ (-14).
Edit1:
Now I am trying to convert it to fsolve:
root9d.m
function F = root9d(c0,c1,c2,c3,c4,c5,c6,c7,c8)
F = [ c0+c4+c8-3.96*2;
c1+c4+c5+c6+2*c7+2*c8-3.96;
c2+c5-20/56;
c3+c6+c7+c8-20/56;
c1*c0*10^(198/100)-c4;
c1*c2*10^(225/100)-c5;
c1*c3*10^(404/100)-c6;
c1^2*c3*10^(538/100)-c7;
c1^2*c0*c3*10^(81/10)-c8;
2*c1(1)+c4(1)+c6(1)-2*c2(1)-3*c3(1)-c6(1)-c0(1)
];
end
and
fun = #root9d;
x0 = [0,0,0,0,0,0,0,0,0];
x = fsolve(fun,x0)
getting
Error using root9d (line 2)
Not enough input arguments.
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Edit2:
am trying vpasolve now
ca=3.96;
c0t(1)=2*ca/10;
c1t(1)=ca;
c2t(1)=20/56;
c3t(1)=20/56;
syms c0 c1 c2 c3 c4 c5 c6 c7 c8
[sol_c0, sol_c1, sol_c2, sol_c3, sol_c4, sol_c5, sol_c6, sol_c7, sol_c8] = vpasolve([c0(1)+c4(1)+c8(1) == c0t(1), c1(1)+c4(1)+c5(1)+c6(1)+2*c7(1)+2*c8(1) == c1t(1), c2t(1)==c2(1)+c5(1), c3t(1)==c3(1)+c6(1)+c7(1)+c8(1), c4(1)==c1(1)*c0(1)*10^1.98, c5(1)==c1(1)*c2(1)*10^2.25, c6(1)==c1(1)*c3(1)*10^4.04, c7(1)==c1(1)^2*c3(1)*10^5.38, c8(1)==c1(1)^2*c0(1)*c3(1)*10^8.1], [c0, c1, c2, c3, c4, c5, c6, c7, c8])
with 9 results like this
sol_c0 =
-2.1773252072885945825011626975607
-0.002452126418901470788857646210148
- 0.18134710731268774000741555758302 - 0.51377486965659220019753505021528i
-1.7768042958209364363265199765376
1.0547920479103151637363730741365
0.0026459489994495584563346427370604
0.0006200391937591632994332743093457
0.020935185135197845770208454108444
- 0.18134710731268774000741555758302 + 0.51377486965659220019753505021528i
What do these sol_cx each with 9 results mean, I need only 9 numbers.
Anyway I'm glad I can still write something in matlab...
Thank you, Dan.
one way to solve my problem is this one, but I hope I will find a shorter one.... also sorry for not saying they were supposed to be positives....
index = find(sol_c0>=0);
sol_c0 = sol_c0(index); sol_c1 = sol_c1(index); sol_c2 = sol_c2(index); sol_c3 = sol_c3(index);sol_c4 = sol_c4(index);sol_c5 = sol_c5(index);sol_c6 = sol_c6(index);sol_c7 = sol_c7(index);sol_c8 = sol_c8(index);
and all this for every sol_
I have a module that takes three inputs, each of which is three bits wide.
output = f(inputA, inputB, inputC)
The output depends on the values of the three inputs but does not depend on their order.
i.e. f(inputA, inputB, inputC) = f(inputB, inputC, inputA)
The solution should work well for both FPGAs and ASICs. Currently I am implementing it without taking advantage of the symmetry, but I assume that explicitly forcing the synthesizer to consider the symmetry will result in a better implementation.
I am planning on implementing this using a hash, h, of the three inputs that does not depend on their order. I can then do:
hash <= h(inputA, inputB, inputC);
output <= VALUE0 when hash = 0 else
VALUE1 when hash = 1 else
.....
My question is what should I use for the hash function?
My thoughts so far:
If each input is 3 bits wide there are 512 possibilities, but only 120 when you consider the symmetry, so theoretically I should be able to use a hash that is 7 bits wide. Practically it may need to be longer.
Each bit of the hash is a function of the input bits and must respect the symmetry of the three inputs. The bits of the hash should be independent from one another. But I'm not sure how to generate these functions.
As mentioned in your question, you could sort and concatenate your inputs.
In pseudo code:
if (A < B)
swap(A, B);
if (B < C)
swap(B, C);
if (A < B)
swap(A, B);
As block diagram:
The 6-in/6-out function needed for a "conditional swap" block:
A3x = A3 B3 ;
A2x = A3 B3' B2 + A3' A2 B3 + A2 B2 ;
A1x = A2 B3' B2' B1 + A3' A2' A1 B2 + A3 A2 B2' B1
+ A2' A1 B3 B2 + A3 B3' B1 + A3' A1 B3 + A1 B1;
B3x = B3 + A3 ;
B2x = A3' B2 + A2 B3' + B3 B2 + A3 A2 ;
B1x = A3' A2' B1 + A1 B3' B2' + A2' B3 B1 + A3 A1 B2'
+ A3' B2 B1 + A2 A1 B3' + A3' B3 B1 + A3 A1 B3'
+ B3 B2 B1 + A3 A2 A1 ;
I have to admit that this solution is not exactly "cheap" and results in a 9-bit hash rather than in a 7-bit hash. Therefore, a look-up table might in fact be the best solution.