Failing to solve a simple least squares fit with Ruby GSL - ruby

I have the following ruby script, running with rb-gsl (1.16.0.6) under ruby-2.2.1
require("gsl")
include GSL
m = GSL::Matrix::alloc([0.18, 0.60, 0.57], [0.24, 0.99, 0.58],
[0.14, 0.30, 0.97], [0.51, 0.19, 0.85], [0.34, 0.91, 0.18])
B = GSL::Vector[1, 2, 3, 4, 5]
qr, tau = m.QR_decomp
x, res = qr.QR_lssolve(tau,B)
The resulting error is:
testls.rb:9:in QR_lssolve: Ruby/GSL error code 19, matrix size must match >solution size (file qr.c, line 193), matrix/vector sizes are not conformant
(GSL::ERROR::EBADLEN)
from testls.rb:9:in
I think my matrices have the right dimensions for an over-determined LS problem, so I can't understand the error message.
In Matlab, I can write:
m=[[0.18, 0.60, 0.57]; [0.24, 0.99, 0.58];...
[0.14, 0.30, 0.97]; [0.51, 0.19, 0.85]; [0.34, 0.91, 0.18]];
B=[1, 2, 3, 4, 5]';
x=m\B
and get
x =
8.0683
0.8844
0.2319
I would like to make the matrix/vector sizes conformant and still express an over-determined problem. It would seem from the GSL documentation that
gsl_linalg_QR_lssolve (const gsl_matrix * QR, const gsl_vector * tau, const
gsl_vector * b, gsl_vector * x, gsl_vector * residual)
is well-suited to task at hand, so is it the binding to Ruby that is broken or my understanding of the correct usage? All help will be appreciated.

Answering my own question: It seems you have to preallocate vectors for the solution x, and the residual and pass them as arguments:
require("gsl")
include GSL
m = GSL::Matrix::alloc([0.18, 0.60, 0.57], [0.24, 0.99, 0.58],
[0.14, 0.30, 0.97], [0.51, 0.19, 0.85], [0.34, 0.91, 0.18])
B = GSL::Vector[1, 2, 3, 4, 5]
qr, tau = m.QR_decomp
x = GSL::Vector.alloc(3)
r = GSL::Vector.alloc(5)
qr.QR_lssolve(tau,B,x,r)
p x
This does indeed yield
GSL::Vector
[ 8.068e+00 8.844e-01 2.319e-01 ]
Charles.

Related

Halide Func reshape

I want to convert the dimensions of a Halide Func. For example consider the following,
func1(x, y, z) = some operation
I know the range of each dimension of the function. Lets assume they are
x = [0, 3], y = [0, 3], z = [0, 15]
Now if I want to convert the func to 1 dimension, something like this
flatten(z * 16 + y * 4 + x) = func1(x, y, z)
But the above definition is not valid as the function definition does not have a pure Var. Is there anyway I can do this in Halide?
You'll need to express it as a pure function of one variable, e.g.:
flatten(x) = func1(x % 4, (x % 16) / 4, x / 16);
You can simplify away much of this added addressing arithmetic by scheduling flatten appropriately, for example:
// Require the bounds realized to be a multiple of 16.
flatten.align_bounds(x, 16);
// Split the loops at the "critical points" of the addressing arithmetic.
flatten
.unroll(x, 4, TailStrategy::RoundUp)
.split(x, xo, xi, 4, TailStrategy::RoundUp);

How to visualize learned filters on tensorflow

Similarly to the Caffe framework, where it is possible to watch the learned filters during CNNs training and it's resulting convolution with input images, I wonder if is it possible to do the same with TensorFlow?
A Caffe example can be viewed in this link:
http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb
Grateful for your help!
To see just a few conv1 filters in Tensorboard, you can use this code (it works for cifar10)
# this should be a part of the inference(images) function in cifar10.py file
# conv1
with tf.variable_scope('conv1') as scope:
kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64],
stddev=1e-4, wd=0.0)
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
bias = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv1)
with tf.variable_scope('visualization'):
# scale weights to [0 1], type is still float
x_min = tf.reduce_min(kernel)
x_max = tf.reduce_max(kernel)
kernel_0_to_1 = (kernel - x_min) / (x_max - x_min)
# to tf.image_summary format [batch_size, height, width, channels]
kernel_transposed = tf.transpose (kernel_0_to_1, [3, 0, 1, 2])
# this will display random 3 filters from the 64 in conv1
tf.image_summary('conv1/filters', kernel_transposed, max_images=3)
I also wrote a simple gist to display all 64 conv1 filters in a grid.

Matrix derivative doesn't get evaluated

I'm trying to evaluate the partial derivative of the most general 3D rotation matrix, like this:
phi, psi, theta = sympy.symbols("phi, psi, theta")
RMatrixPhi = sympy.Matrix([[cos(phi), sin(phi), 0],
[-sin(phi), cos(phi), 0],
[0, 0, 1]])
RMatrixPsi = sympy.Matrix([[cos(psi), 0, sin(psi)],
[0, 1, 0 ],
[-sin(psi), 0, cos(psi)]])
RMatrixTheta = sympy.Matrix([[1, 0, 0 ],
[0, cos(theta), sin(theta)],
[0, -sin(theta), cos(theta) ]])
RMatrix = RMatrixPhi * RMatrixPsi * RMatrixTheta
D = diff(RMatrix, phi)
However,D is then a sympy.Derivative object, and I cannot get it evaluated,
it's just printed out as Derivative(Matrix(...))
The only way I could get it working is by writing
sympy.Matrix([sympy.diff(r, phi) for r in RMatrix]).reshape(3,3)
but that looks ugly. What's the right way to compute such derivatives?
The Matrix class has a method called diff which, according to the documentation ...
Docstring:
Calculate the derivative of each element in the matrix.
So use
RMatrix.diff(phi)
to perform element-wise derivation.

MATLAB - Sort 2D points ensuring adjacent points differ by one coordinate? [duplicate]

I have 2 vectors that are x and y coordinates of the 8 vertexes of a polygon
x=[5 5 7 7 9 9 5 7]
y=[8 6 6 8 6 8 10 10]
I wanna sort them (clockwise) to obtain the right vectors (to draw the polygon correctly)
x=[5 7 9 9 7 7 5 5]
y=[6 6 6 8 8 10 10 8]
Step 1: Find the unweighted mean of the vertices:
cx = mean(x);
cy = mean(y);
Step 2: Find the angles:
a = atan2(y - cy, x - cx);
Step 3: Find the correct sorted order:
[~, order] = sort(a);
Step 4: Reorder the coordinates:
x = x(order);
y = y(order);
Python version (numpy) for Ben Voigt's algorithm:
def clockwise(points):
x = points[0,:]
y = points[1,:]
cx = np.mean(x)
cy = np.mean(y)
a = np.arctan2(y - cy, x - cx)
order = a.ravel().argsort()
x = x[order]
y = y[order]
return np.vstack([x,y])
Example:
In [281]: pts
Out[281]:
array([[7, 2, 2, 7],
[5, 1, 5, 1]])
In [282]: clockwise(pts)
Out[282]:
array([[2, 7, 7, 2],
[1, 1, 5, 5]])
I tried the solutions by #ben-voight and #mclafee, but I think they are sorting the wrong way.
When using atan2 the angles are stated in the following way:
Matlab Atan2
The angle is positive for counter-clockwise angles (upper half-plane,
y > 0), and negative for clockwise angles (lower half-plane, y < 0).
Wikipedia Atan2
This means that using ascending sort() of Numpy or Matlab will progress counterclockwise.
This can be verified using the Shoelace equation
Wikipedia Shoelace
Python Shoelace
So, adjusting the answers mentioned above to use descending sorting the correct solution in Matlab is
cx = mean(x);
cy = mean(y);
a = atan2(y - cy, x - cx);
[~, order] = sort(a, 'descend');
x = x(order);
y = y(order);
The solution in numpy is
import numpy as np
def clockwise(points):
x = points[0,:]
y = points[1,:]
cx = np.mean(x)
cy = np.mean(y)
a = np.arctan2(y - cy, x - cx)
order = a.ravel().argsort()[::-1]
x = x[order]
y = y[order]
return np.vstack([x,y])
pts = np.array([[7, 2, 2, 7],
[5, 1, 5, 1]])
clockwise(pts)
pts = np.array([[1.0, 1.0],
[-1.0, -1.0],
[1.0, -1.0],
[-1.0, 1.0]]).transpose()
clockwise(pts)
Output:
[[7 2 2 7]
[5 1 5 1]]
[[2 7 7 2]
[5 5 1 1]]
[[ 1. -1. 1. -1.]
[ 1. -1. -1. 1.]]
[[-1. 1. 1. -1.]
[ 1. 1. -1. -1.]]
Please notice the [::-1] used to invert arrays / lists.
This algorithm does not apply to non-convex polygons.
Instead, consider using MATLAB's poly2cw()

Logarithmically spacing number

I would like to test several values of intensity.
I need them to be spaced logarithmically from 1 to 1000. Yet I just use 1, 10, 100, 1000, but I would like to have more data point, let`s say 10.
How could I find 10 logarithmically spaced number between 1 and 1000 in Mathematica ?
If a is start, c is end and b is number of intervals:
{a, b, c} = {1, 10, 1000};
t = (c/a)^(1/b) // N
a*t^Range[b]
1.99526
{1.99526, 3.98107, 7.94328, 15.8489, 31.6228, 63.0957, 125.893, 251.189, 501.187, 1000.}
I used N just to see better, what do we have.
Here is one way:
In[11]:= base = Block[{a}, a /. NSolve[a^9 == 1000, a][[-1, 1]]]
Out[11]= 2.15443
In[13]:= base^Range[0, 9]
Out[13]= {1., 2.15443, 4.64159, 10., 21.5443, 46.4159, 100.,
215.443,464.159, 1000.}
EDIT
Here is a much shorter and more direct way to get the same:
In[18]:= N[10^Range[0, 3, 1/3]]
Out[18]= {1., 2.15443, 4.64159, 10., 21.5443, 46.4159, 100.,
215.443, 464.159, 1000.}
Solve the equation x ** 9 = 1000 -- then your numbers are: x ** 0, x ** 1, ... x ** 9.
note: where x ** y means x to the power of y

Resources