Writing {value} +- {uncertainty} in matrix. How does that work with sympy? - matrix

I have two 6x6 matrices. Here I'll just show the first two rows and columns:
import sympy as sp
values = sp.Matrix([[-0.403900000000000, 0.158730000000000], [-1.52350000000000, -1.87937000000000]])
uncertainties = sp.Matrix([[0.000600000000000000, 0.000280000000000000], [0.000270000000000000, 0.000170000000000000]])
I want to get a matrix where the [0][0] element would look like "-0.4039 +- 0.0006". And this for every element, so that i can export this matrix to latex.
How do I do this?
I tried using
f"{values[i][j]} +- {uncertainties[i][j]}"
for all i and j, but that just gave me value[i][j] - uncertainty[i][j]
I couldn't find this on the internet. Do I need to look into latex for loops? Or can I do that in sympy?
I jsut really really don't want to manually put the uncertainty behind every matrix element in multiple matrices.
Thanks!

Matrix cannot hold strings but it can hold Symbols with arbitrary names. Does the following produce the right latex?
>>> str(Float(.4309).round(4))+' +- 0.006'
'0.4309 +- 0.006'
>>> Matrix([[Symbol(_)]])
Matrix([[0.4309 +- 0.006]])
If so then it's just a matter of knowing what decimal to round to and then putting the Symbols in place. I would probably have a values matrix and an uncertainties matrix and a function that puts them together and outputs the special symbol matrix. The uncertainties package might be of use in creating the strings.

Related

Control print order of matrix terms in Sympy

I have a matrix addition with several terms that I want to display in a Jupyter Notebook. I need the order of terms to match the standard notation - in my case, of linear regression. But, the terms do not, by default, appear in the correct order for my purpose, and I would like to ask how to control the order of display of matrices in a matrix addition (MatAdd) term in Sympy. For example, here we see that Sympy selects a particular order for the terms, that appears to be based on the values in the Matrix.
from sympy import MatAdd, Matrix
A = Matrix([1])
B = Matrix([0])
print(MatAdd(A, B, evaluate=False))
This gives
Matrix([[0]]) + Matrix([[1]])
Notice the matrix terms do not follow the order of defintion or the variable names.
Is there anything I can do to control the print output order of Matrix terms in a MatAdd expression?
You can use init_printing to chose from a few options. In particular, the order keyword should control how things are shown on the screen vs how things are stored in SymPy objects.
Now comes the differences: by setting init_printing(order="none") printers behave differently. I believe this is some bug.
For example, I usually use Latex rendering when using Jupyter Notebook:
from sympy import MatAdd, Matrix, init_printing
init_printing(order="none")
A = Matrix([1])
B = Matrix([0])
add = MatAdd(A, B, evaluate=False)
print(add)
# out: Matrix([[0]]) + Matrix([[1]])
display(add)
# out: [1] + [0]
Here you can see that the latex printer is displaying the elements as they are stored (check add.args), whereas the string printer is not following that convention...

Squeak: Creating a Matrix with Specified Rows and Columns

I'm just trying to do an assignment in Squeak (Smalltalk), and I can't figure out how to create a matrix with a specific number of rows and columns.
I know that one can create a 3x3 matrix with something like A := Matrix new:3., but I need something like a 3x2 matrix. The System Browser isn't much help to my understanding of how to do this, and I've been searching the internet for awhile and haven't found much of anything to help. Could anyone give an example line of code of how to create something like a 3x2 matrix?
What you should understand is how Smalltalk deals with constructors - always check the Class class.
If you check the Matrix class you will find out that the #new: internally uses ^ self rows: dimension columns: dimension. That means your:
aMatrix := Matrix new:3
actually does
aMatrix := Matrix rows: 3 columns: 3, which happens to happen to be what you are searching for.
In my eyes, the #new: message is not a good choice. I would prefer to have a #squareSize: or something like it.
Note: Don't use capitals for variables (comment based on your A := assigment)
Edit: Why not to use capitals for local/instance variables?
The capital letter at the beginning is reserved for global variables. You have such like Transcript, Smalltalk, etc. For example, class names are also globals that is why you have Matrix and not matrix. For class instance you use previx with a or an like I have used aMatrix is an instance of class Matrix.
There can be suttle differences among different Smalltalk flavours. For example, in Smalltalk/X the class variable starts with a capital letter, which is, as far I could see, not the case in GNU Smalltalk.
I now have the solution, for anyone else ever wondering about how to do this. It's
var := Matrix rows: A columns: B. to create a matrix (A x B), where A and B are integers.

How to normalize a vector used for features extraction from an image?

My actual vector has 110 elements that I'll use to extract features from images in matlab, I took this one (tb) to simplify
tb=[22.9 30.0 30.3 27.8 24.1 28.2 26.4 12.6 39.7 38.0];
normalized_V = tb/norm(tb);
I = mat2gray(tb);
For normalized_v I got 0.2503 0.3280 0.3312 0.3039 0.2635 0.3083 0.2886 0.1377 0.4340 0.4154.
For I I got 0.3801 0.6421 0.6531 0.5609 0.4244 0.5756 0.5092 0 1.0000 0.9373 which one should I use if any of those 2 methods and why, and should I transform the features vector to 1 element after extraction for better training or leave it as a 110 element vector.
Normalization can be performed in several ways, such as the following:
Normalizing the vector between 0 and 1. In that case, just use:(tb-min(tb))/max(tb)
Making the maximum point at 1. In that case, just use: tb/max(tb) (which is the method that you have been used before).
Making the mean 0 and the standard deviation as 1. This is the most common method for using the returned values as features in a classification procedure and thus, I think that it is the one that you should use right now: zscore(tb) (or (tb-mean(tb))/std(tb)).
So, your final values would be:
zscore(tb)
ans =
-0.6664
0.2613
0.3005
-0.0261
-0.5096
0.0261
-0.2091
-2.0121
1.5287
1.3066
Edit:
In regard to your second question, it depends on the number of observations. Every single classifier takes an MxN matrix of data and an Mx1 vector of labels as inputs. In this case, M refers to the number of observations, whereas N refers to the number of features. Usually, in order to avoid over-fitting, it is recommended to use a number of features less than the tenth part of the number of observations (i.e., the number of observations must be M > 10N).
So, in your case, if you use the entire 110-set of features, you should have a minimum of 1100 observations, otherwise you can have problems with over-fitting.

Using autocorr in Matlab with matrix input?

As described in the help:
http://www.mathworks.co.uk/help/econ/autocorr.html#btzja9w-1
autocorr expects the input y to be a uni-variate time series.
Is there a multi-variate equivalent?
I have an n*m array and I would like to calculate the lag(1) auto correlation along each row.
Obviously I can work round by splitting the matrix up into univariate series and then inputting them individually, but just wondered if there was a neater solution?
Thanks
Baz
I don't have the econometrics toolbox to test this but what about:
bsxfun(#crosscorr, M, permute(M, [1,3,2]))
or maybe
ACF_1 = #(x,y)(crosscorr(x,y,1));
bsxfun(ACF_1, M, permute(M, [1,3,2]))

Details of the "New Yale" sparse matrix format?

There's some Netlib code written in Fortran which performs transposes and multiplication on sparse matrices. The library works with Bank-Smith (sort of), "old Yale", and "new Yale" formats.
Unfortunately, I haven't been able to find much detail on "new Yale." I implemented what I think matches the description given in the paper, and I can get and set entries appropriately.
But the results are not correct, leading me to wonder if I've implemented something which matches the description in the paper but is not what the Fortran code expects.
So a couple of questions:
Should row lengths include diagonal entries? e.g., if you have M=[1,1;0,1], it seems that it should look like this:
IJA = [3,4,4,1]
A = [1,1,X,1] // where X=NULL
It seems that if diagonal entries are included in row lengths, you'd get something like this:
IJA = [3,5,6,1]
A = [1,1,X,1]
That doesn't make much sense because IJA[2]=6 should be the size of the IJA/A arrays, but it is what the paper seems to say.
Should the matrices use 1-based indexing?
It is Fortran code after all. Perhaps instead my IJA and A should look like this:
IJA = [4,5,5,2]
A = [1,1,X,1] // still X=NULL
Is there anything else I'm missing?
Yes, that's vague, but I throw that out there in case someone who has messed with this code before would like to volunteer any additional information. Anyone else can feel free to ignore this last question.
I know these questions may seem rather trivial, but I thought perhaps some Fortran folks could provide me with some insight. I'm not used to thinking in a one-based system, and though I've converted the code to C using f2c, it's still written like Fortran.
I can't see how you deduced those vectors from that paper. First the Old Yale format:
M = [7,16;0,-12]
Then, A contains all non-zero values of M in row-form:
A = [7,16,-12]
and IA stores the position in A of the first elements of each row, and JA stores the column indices of all the values in A:
IA = [1,3,4]
JA = [1,2,2]
New format: A has diagonal values first, a zero and then the remaining non-zero elements (I have put | to clarify the seperation between diagonal and non-diagonal) :
A = [7,-12,0 | 16]
IA and JA are combined in IJA, but as far as I can tell from the paper you need to take into account the new ordering of A (I have put | to clarify the seperation between IA and JA):
IJA = [1,2,3 | 2]
So, applied to your case M = [1,1;0,1], I get
A = [1,1,0 | 1]
IJA = [1,2,3 | 2]
first element of the first row is the first in A and the first element of the second row is the second in A, then I put 3 since they say the length of a row is determined by IA(I)-IA(I+1), so I make sure the difference is 1. Then the column indices of the non-zero non-diagonal elements follow, and that is 2.
So, first of all, the reference given in the SMMP paper is possibly not the correct one. I checked it out (the ref) from the library last night. It appears to give the "old Yale" format. It does mention, on pp. 49-50, that the diagonal can be separated out from the rest of the matrix -- but doesn't so much as mention an IJA vector.
I was able to find the format described in the 1992 edition of Numerical Recipes in C on pp. 78-79.
Of course, there is no guarantee that this is the format accepted by the SMMP library from Netlib.
NR seems to have IA giving positions relative to IJA, not relative to JA. The last position in the IA portion gives not the size of the IJA and A vectors, but size-1, because the vectors are indexed starting at 1 (per Fortran standard).
Row lengths do not include non-zero diagonal entries.

Resources