Degeneracy of a diagonal matrix
What is the degeneracy of diagonal matrix A A =2 0 0 0 0 0 0 2 1 0 0 0 0 0 2 0 0 0 0 0 0 3 1 0 0 0 0 0 3 1 0 0 0 0 0 3 Can anyone help
See also questions close to this topic

Multiplying Boolean Matrices
I am trying to explicitly multiple 2 Boolean matrices, regardless of the number of rows or columns.
For example if
A = [[0, 1, 1], [1, 0, 1]]
andB = [[1, 0], [0, 0], [0, 1]],
then the product is[[0, 1], [1, 1]]
.Edit: I want to this explicitly without the
numpy
. 
Matrix multiplication in R outputs list and not matrix?
I am trying to use linear algebra to calculate:
A < Z%*%diag(DMIdiag)
where Z is a large matrix of countries and diag(DMIdiag) is the inverse of another matrix of countries. By this matrix multiplication, A should also become a matrix. But my end product looks like this:
V1 Albania NaN Algeria NaN Antigua and Barbuda NaN Argentina NaN Armenia NaN Aruba NaN Australia NaN
How can this be?
Here are dput's of Z:
dput(Z[1:50, c(1, 2, 3, 4, 5, 6)]) structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23664.36, 9.6, 162.18, 0, 0, 331.86, 0, 0, 0, 0, 104590.08, 0, 0.6, 0, 0, 0, 0, 0, 0, 0, 0, 7140, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14264.04, 1541.7, 107.28, 4747.86, 0, 3759.9, 0, 0, 0, 107.94, 0, 0, 0, 0, 0, 0, 15.48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(50L, 6L), .Dimnames = list( c("Albania", "Algeria", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "CÃ´te d'Ivoire", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic", "Chile", "China, Hong Kong SAR", "China, Macao SAR", "China, mainland", "China, Taiwan Province of", "Colombia", "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia", "Democratic Republic of the Congo", "Denmark", "Djibouti"), c("Albania", "Algeria", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba")))
and DMIdiag:
> dput(DMIdiag[1:50, c(1, 2, 3, 4, 5, 6)]) structure(c(0.000349437754652764, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7.33421842681308e06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.54170908936178e05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.00602228244504667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.75370799726505e09, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0128650456709121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(50L, 6L))

Inverse of a diagonal matrix in r
I would like to take the inverse of this subsetted diagonal matrix (I hope I have created a dput in the right way):
dput(DMIdiag[1:20,1:20]) structure(c(581.166666666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5397.42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1254652.485, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15164.7616666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3721176.8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1805.11333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 266406189.456667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 478590.468333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 508510.586666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 348034.096666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 945785.841666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1056505.53666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1389813.64166667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28585.2133333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 713675.006666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 661456.686666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53533.9066666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7291.31666666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49375.4466666667, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 807758.686666667 ), .Dim = c(20L, 20L))
My code:
DMI < p + rowSums(Z, na.rm = TRUE)*1 #p is a vector and Z is a matrix DMIdiag < diag(DMI) #produces the matrix above DMIinv < solve(DMIdiag)
Running the last line gives me:
Error in solve.default(DMIdiag) : Lapack routine dgesv: system is exactly singular: U[173,173] = 0
I don't really understand how this matrix be singular, if this is even the problem?

Is there a way to sum a matrix entry and it's reflection across a diagonal along a given axis in a numpy array?
Suppose
A = np.array([1,2,0,3,5,0,0],[5,6,7,0,9,5,10])
(The actual data set in this case is a square matrix with a shifted diagonal of zeros) I want to create an np array V that sums the pairs reflected across the first zero in each row, in the order of their distance from the zero. i.e., V[0]=[(2+3),(1+5),0] V[1] =[(7+9),(6+5),(10+5)]...
So
V = ([5,6,0],[16,11,15])
I can accomplish this in a very rudimentary way by looping through each row and then adding each row to a shifted version of its reflection, with just two rows it would be something simple like this.
B = A.copy() B = B[..,::1] V = np.array([A[0]+np.roll(B[0],2),A[1]+B[1]])[:,0:3]
However I'm wondering if there is a vectorized approach to this operation, or some function native to numpy that sums an entry of an array along with its reflection across its diagonal because as the data set gets large this becomes less efficient.

VBA program for a given square matrix of the user specified size determines the diagonal with the smallest sum of elements?
How can I write this code? I couldn't figure it out yet

Trying to fill diagonal without numpy
I am trying to print and fill in 2D python arrays' diagonals with values:
test = [ [1, 5, 6], [20, 21, 32], [17, 19, 34] ] for i in range(3): print(test[i][i])
Now as I expect, this prints:
1 21 34
Now let's say I want to fill a matrix's diagonal with numbers through the code below:
memo = [[0]*3]*3 print(memo) for i in range(3): memo[i][i] = 1 print(memo)
The output of the initial and consequent matrices is below:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]] [[1, 0, 0], [1, 0, 0], [1, 0, 0]] [[1, 1, 0], [1, 1, 0], [1, 1, 0]] [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
The result should be
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
. Not really sure why this is the case?