Padding matrix for spacial filtering - image-processing

for examples, I have a 2x2 matrix
1 2
3 4
Then I pad it with 2, it becomes,
x x x x x x
x x x x x x
x x 1 2 x x
x x 3 4 x x
x x x x x x
x x x x x x
Then I use border_replicate to fill value to x
x x 1 2 x x
x x 1 2 x x
1 1 1 2 2 2
3 3 3 4 4 4
x x 3 4 x x
x x 3 4 x x
The problem is for the x that located at the vertex of the new matrix, if I use border_replicate, what will their value? ....
Thank you very much

Based on your example, it looks like the corners (clockwise from top left) would be 1, 2, 4, and 3.

Related

1 and 2 and ((3 AND 4) or 5 or (6 and 7)) Can any one help writing logic to expand expression and list out all possible expressions? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
1 and 2 and ((3 AND 4) or 5 or (6 and 7)) Can any one help writing logic to expand expression and list out all possible expressions?
Eg.,
Result:
1 and 2 and 3 and 4
1 and 2 and 5
1 and 2 and 6 and 7
I interpreted the question as follows. Suppose x1, x2,..., x7 are seven variables, each having a value of true or false. List the combinations of values of variables that result in
x1 and x2 and ((x3 and x4) or x5 or (x6 and x7))
evaluating true.
We can obtain the desired results as follows.
arr = [true, false].repeated_permutation(7).
select {|x1,x2,x3,x4,x5,x6,x7| x1 and x2 and ((x3 and x4) or x5 or (x6 and x7))}
#=> [[true, true, true, true, true, true, true],
# [true, true, true, true, true, true, false]]
# ...
# [true, true, false, false, false, true, true]]
See Array#repeated_permutation.
To make the results easier to visualize we can display a table that shows which variables are true for each of the 23 elements of arr:
puts "x1 x2 x3 x4 x5 x6 x7"
puts "--------------------"
arr.each { |a| puts a.map { |tf| tf ? "X " : " " }.join }
displays
x1 x2 x3 x4 x5 x6 x7
--------------------
X X X X X X X
X X X X X X
X X X X X X
X X X X X
X X X X X X
X X X X X
X X X X X
X X X X
X X X X X X
X X X X X
X X X X X
X X X X
X X X X X
X X X X X X
X X X X X
X X X X X
X X X X
X X X X X
X X X X X
X X X X
X X X X
X X X
X X X X
Note that x1 and x2 both evaluate true for each element of arr. Also, there are 2**4 #=> 16 combinations for which x5 is true, as x3, x4, x6 and x7 can each be either true or false when x5 is true.
Since all expressions are literals, there are no possible different results, and this expression will always evaluate to 4:
1 and 2 and ((3 and 4) or 5 or (6 and 7))
1 and 2 and ( 4 or 5 or (6 and 7))
1 and 2 and ( 4 or 5 or 7 )
1 and 2 and 4 or 5 or 7
2 and 4 or 5 or 7
4 or 5 or 7
4 or 7
4

Why is Maxima failing to give a solution?

I have a function in Maxima I am differentiating then attempting to find the value at which this is zero. When I use solve(), however, I am not given a solution. Why is this, and how can I work around it?
(%i1) f(x):=(-5*(x^4+5*x^3-3*x))/(x^2+1);
(%o1) f(x):=((-5)*(x^4+5*x^3+(-3)*x))/(x^2+1)
(%i2) df(x):=''(diff(f(x), x));
(%o2) df(x):=(10*x*(x^4+5*x^3-3*x))/(x^2+1)^2-(5*(4*x^3+15*x^2-3))/(x^2+1)
(%i3) solve(df(x), x);
(%o3) [0=2*x^5+5*x^4+4*x^3+18*x^2-3]
The function solve is not too strong; there are many problems it can't solve. A stronger version is under development. In the meantime, try the add-on package to_poly_solve. Here's what I get:
(%i1) df(x) := (10*x*(x^4+5*x^3-3*x))/(x^2+1)^2-(5*(4*x^3+15*x^2-3))/(x^2+1) $
(%i2) load (to_poly_solve) $
(%i3) to_poly_solve (df(x), x);
(%o3) %union([x = - 2.872468527640942], [x = - 0.4194144025323134],
[x = 0.3836388367122223], [x = 0.2041221431132173 - 1.789901606296292 %i],
[x = 1.789901606296292 %i + 0.2041221431132173])
Something which is maybe a little surprising is that to_poly_solve has returned a numerical solution instead of exact or symbolic. Tracing allroots shows that to_poly_solve has constructed a quintic equation and punted it to allroots. Since the general quintic doesn't have a solution in terms of radicals, and even in special cases it's probably very messy, maybe it's most useful to have a numerical solution anyway.
Try plot2d(df(x), [x, -3, 1]) to visualize the real roots returned above.
You can try to find a numerical solution. I don't know why solve does not try this. Either you take the ouput of aolveor you do hte folölowing:
(%i1) f(x):=(-5*(x^4+5*x^3-3*x))/(x^2+1);
4 3
(- 5) (x + 5 x + (- 3) x)
(%o1) f(x) := ---------------------------
2
x + 1
(%i2) df(x):=''(diff(f(x), x));
4 3 3 2
10 x (x + 5 x - 3 x) 5 (4 x + 15 x - 3)
(%o2) df(x) := ---------------------- - --------------------
2 2 2
(x + 1) x + 1
Bring it to a common denominator and extract the numerator:
(%i3) xthru(df(x));
4 3 2 3 2
10 x (x + 5 x - 3 x) - 5 (x + 1) (4 x + 15 x - 3)
(%o3) ------------------------------------------------------
2 2
(x + 1)
(%i4) num(%);
4 3 2 3 2
(%o4) 10 x (x + 5 x - 3 x) - 5 (x + 1) (4 x + 15 x - 3)
use allsrootsto find the roots of a polynomial numerically
(%i5) allroots(%);
(%o5) [x = 0.3836388391066617, x = - 0.4194143906217701,
x = 1.789901606296292 %i + 0.2041221431132174,
x = 0.2041221431132174 - 1.789901606296292 %i, x = - 2.872468734711326]
skip the complex solutions
(%i6) sublist(%,lambda([t],imagpart(rhs(t))=0))
;
(%o6) [x = 0.3836388391066617, x = - 0.4194143906217701,
x = - 2.872468734711326]

Substitute variable in Maxima

newbie Maxima question
I have a transfer function in Maxima
E1 : y = K_i*s/(s^2 + w^2);
I'd like to have the closed-form of the equation affter applying the bilinear transform
E2 : s = (2/Ts*(z-1)/(z+1));
I would like to get the transfer function for z, by substituing s by equation E2. How should I proceed?
Regards
Note that subst can apply one or more substitutions stated as equations. In this case, try subst(E2, E1).
That will probably create a messy result -- you can simplify it somewhat by applying ratsimp to the result.
Here's what I get from that.
(%i2) E1 : y = K_i*s/(s^2 + w^2);
K_i s
(%o2) y = -------
2 2
w + s
(%i3) E2 : s = (2/Ts*(z-1)/(z+1));
2 (z - 1)
(%o3) s = ----------
Ts (z + 1)
(%i4) subst (E2, E1);
2 K_i (z - 1)
(%o4) y = ------------------------------
2
4 (z - 1) 2
Ts (z + 1) (------------ + w )
2 2
Ts (z + 1)
(%i5) ratsimp (%);
2
2 K_i Ts z - 2 K_i Ts
(%o5) y = -----------------------------------------------
2 2 2 2 2 2 2
(Ts w + 4) z + (2 Ts w - 8) z + Ts w + 4

Is there an equivalent bsxfun in TensorFlow as there in Matlab?

I was trying to translate the following MATLAB code to tensorflow:
WW = sum(W.^2, 1); % ( 1 x D^(l)= sum( (D^(l-1) x D^(l)), 1 )
XX = sum(A.^2, 2); % (M x 1) = sum( (M x D^(l-1)), 2 )
bsxfun(#plus, WW, XX) ; % (M x D^(l)) - (M x D^(l)) = (M x D^(l-1)) * (D^(l-1) x D^(l)) - (M x D^(l))
which is very simple MATLAB code and was wondering if there was an equivalent code in TensorFlow. Ideally, W and/or X should be tf.Variable(init) variables because I'd like to compute the derivatives with respect to each variable.
Tensorflow, like NumPy, does broadcasting.
You can do
WW + XX
and it'll figure out the sizes itself
See the documentation here

Torch - Randomly choose dimension of tensor to slice

I have a torch tensor of size (1 x n x n x n) and I would like to randomly choose one of the last 3 dimensions to randomly slice at s and then do. For example it could output the below tensors with equal probability;
(1 x s x n x n)
(1 x n x s x n)
(1 x n x n x s)
I realise I could just do a few if else statements but I am curious if there is a "neater" option using a function like torch.random(1,4) to select the dimension.
assuming that you want to narrow a block of s elements randomly, out of n elements.
Let's use :narrow.
n = 100
s = 20
x = torch.randn(1, n, n, n)
y = x:narrow(torch.random(2, 4), torch.random(1, n - s + 1), s)

Resources