Matrix calculations in rails app - ruby

In my rails app I need to do the following:
Create a form for inputting the data for a 4x4 matrix, store this data in a model
Use a ruby matrix for calculating the cholesky decomposition of this matrix, and then displaying the resulting decomposed matrix in a view
From what I understand the form data is stored in a 1-dimensional array, I need the data stored in a 4x4 array but I haven't seen any examples of this. What is the best way to do this?

You may want to look at the following tools:
standard Matrix class
ruby gsl gem
extendmatrix gem

Related

Multiplying matrices across tenor axis with numpy and with GPU

I have a matrix X with shape (F,T,M). I wish to multiply each (T,M) matrix along the F axis so that the answer will be of shape (M,M,F). This code does the job for me but this operation repeats many times and it is very slow:
for f in range(F):
output[:,:,f] = np.matmul(X[f,:,:].T,X[f,:,:])
All I could find is np.tensordot() function. If I understand correctly, this is not a good option for me since I need a matrix multiplication and not a dot product.
How do I implement this efficiently using numpy? Is it possible and beneficial to utilize keras\tf for this purpose?
We can use np.matmul/# opeartor in Python 3.x after extending dimensions -
np.matmul(X.swapaxes(1,2),X).swapaxes(0,2)
(X.swapaxes(1,2)#X).swapaxes(0,2)
Alternatively, with np.einsum with a direct translation off the shape variables used for the string notation -
np.einsum('ftm,ftn->mnf',X,X)

3D triangulation using HALCON

My aim is to calibrate a pair of cameras and use them for simple measurement purposes. For this purpose, I have already calibrated them using HALCON and have all the necessary intrinsic and extrinsic camera Parameters. The next step for me is to basically measure known lengths to verify my calibration accuracies. So far I have been using the method intersect_lines_of_sight to achieve this. This has given me unfavourable results as the lengths are off by a couple of centimeters. Is there any other method which basically triangulates and gives me the 3D coordinates of a Point in HALCON? Or is there any leads as to how this can be done? Any help will be greatly appreciated.
Kindly let me know in case this post Needs to be updated with code samples
In HALCON there is also the operator reconstruct_points_stereo with which you can reconstruct 3D points given the row and column coordinates of a corresponding pixel. For this you will need to generate a StereoModel from your calibration data that is then used in the operator reconstruct_points_stereo.
In you HALCON installation there is an standard HDevelop example that shows the use of this operator. The example is called reconstruct_points_stereo.hdev and can be found in the example browser of HDevelop.

How to model a time dependant matrix with RNN?

I am working on video classification. So let's say, I sub-sample the video temporally. So for each sub-sample, I engineer some features out of it. Let's say I can represent these features with a two dimensional matrix.
So, these values of the matrix are time-Dependant. So for each video, I have a set of matrices, which are time dependent.
So I need to use a RNN to model these time dependent matrix values and represent the video. This representation should classify the video in to classes. In other words, the RNN should be able to classify the video in to classes, depending on these time dependent matrix values.
I need to know, Is this possible with RNNs? would it be a good practice? If so, what are the guide lines anyone could provide me. What is a good library to use? What would be good tutorials? Thanks.
You flatten the images of the video and use them as an element of the sequence.
You will have to put a convnet under the RNN, most likely, to get reasonable results.
As such, you feed the image to a convnet, then flatten the activation map and feed it to the RNN cell.
https://stackoverflow.com/a/36992625/447599

Unable to use multiplication with grayscale image matrix

I am quite new to matlab/octave. I loaded an image using imread() function of octave. I tried to perform multiplication operation on the matrix but get the following error:
binary operator `*' not implemented for `uint8 matrix' by `matrix' operations
Is there another way to input the image??
I=imread('...');
I=rgb2gray(I);
I=double(I);
% you can perform the multiplication operation now
This usually means that you are trying to multiply arrays of different data types. The easiest thing to do here is to convert the uint8 image into double. You can either use the double() function, which will just cast the values, or us im2double(), which will also normalize the values to be between 0 and 1.
If you're trying to multiple two images (I'm guessing that's what you're trying to do since the error is for multiplying matrices and not a matrix by a scalar), you should use the immultiply function which handles different classes for you.
Simply, use immultiply (imgA, imgB) and never again worry about what class are imgA and imgB. Note that you'll need the image package installed in loaded.

generating image features dataset in scikit-learn - csv file

I extract 2 edge features (Hog feature and sobel operator) from a single image.
How can i create an image feature dataset in Scikit-learn python, like iris_dataset ?
In the library there are csv files which represent datasets. A csv file containing only numbers. How were generate these numbers? feature extraction?
unfortunately i saw only a java tutorial here http://www.coccidia.icb.usp.br/coccimorph/tutorials/Tutorial-2-Creating-..., at 5 point talk about generating the training matrices (average and co-variance matrices)?
There is any function in Scikit who generate these training arrays?
You don't need to wrap your data as a CSV file to load it as a dataset. scikit-learn models have a fit method that expects:
as first argument that is a regular numpy array (or scipy.sparse matrices) with shape (n_samples, n_features) (most often with dtype=numpy.float64) to encode the features vector for each sample in the training set,
and for supervised classification models, a second argument with shape (n_samples,) and dtype=numpy.int32 to encode the class label assignments encoded as integer values for each sample of the training set.
If you don't know the basic numpy datastructure and what shape and dtype mean, I stongly advise you to have a look at a tutorial such as SciPy Lecture Notes.
Edit: If you really need to read / write numerical CSV to / from numpy arrays, you can use numpy.loadtxt / numpy.savetxt

Resources