4D array with elements only on one side of the diagonal

In summary: You are correct, a matrix with matrices as elements. I said 4D because in numpy arrays one can specify up to 4 indices if I’m not wrong, and this will give one a matrix of matrices. And yes, the diagonal referred to is the diagonal of the matrix of matrices. If not having any entries below or above this diagonal is more efficient than having simply empty matrices, than a nested list is probably the way to go. Would this list then be a list of numpy arrays, and if so, how would one initialize that?Yes, the nested list would be a list of numpy arrays. You could initialize it as follows:>>> arr = np.
  • #1
schniefen
178
4
TL;DR Summary
Is it possible to initialize a 4D array with only elements on one side of the diagonal?
Given the parameters ##n## and ##m##, I'd like to initialize a ##n \times n ## 4D array where each entry is an ##m \times m ## matrix, and where each column of the matrix is an array of type numpy.linspace(0,1,m). Furthermore, I'd only like to have entries on the diagonal and above it, or only on the diagonal and below it. Is there a possible one-liner for this? The reason for this peculiarly shaped array is that I only need half of the matrices in the full array, but at the same time I’d like to keep the structure the array gives, i.e. the matrices on the second row belong to a “group” and this “group” consists of exactly as many matrices there are in the row of the array made up of only matrices on the diagonal and above or below it. The same goes for the columns.
 
Technology news on Phys.org
  • #2
Which diagonal are you referring to?

It sounds like your structure is not so much a 4D array as a 2D array of matrices - ie a n x n array of elements, each of which is a m x m matrix.

Although not clear, it sounds like you mean the diagonal of the n x n array, rather than a diagonal of a m x m matrix, or a diagonal hyperplane of a 4D hyper-rectangle (which is a complicated beast).

If that's correct, it gives rise to another necessary clarification: what do you mean by "only ... have entries on the diagonal and above it". Do you mean to not have any matrices below the diagonal or perhaps to have all zeroes or NAs in those matrices? If you mean not to have any, I suspect the best structure for you is a nested list, as follows:
- item 1 is a n-element list of m x m matrices
- item 2 is a (n-1)-element list of m x m matrices
.
.
.
- item n is a 2-element list of m x m matrices (ie containing two such matrices)
- item n is a 1-element list of m x m matrices (ie containing one m x m matrix)

Item k of that top-level list is the k-th row of the top-level n x n array.

Is that the sort of thing you want?
 
  • Like
Likes schniefen
  • #3
You are right, a matrix with matrices as elements. I said 4D because in numpy arrays one can specify up to 4 indices if I’m not wrong, and this will give one a matrix of matrices. And yes, the diagonal referred to is the diagonal of the matrix of matrices. If not having any entries below or above this diagonal is more efficient than having simply empty matrices, than a nested list is probably the way to go. Would this list then be a list of numpy arrays, and if so, how would one initialize that?
 
  • #4
I guess numpy 4D arrays only gives a column vector of matrices, and not a matrix of matrices.
 
  • #5
You can have an array of dimension [itex](\frac12n(n+1),m,m)[/itex], in which the [itex](i,j)[/itex]th matrix is at position [itex]j + \frac12i(i+1)[/itex] of the first index, where [itex]0 \leq j \leq i < n[/itex]. You can initialize this as follows:
Python:
>>> import numpy as np
>>> n = 3
>>> m = 2
>>> arr = np.zeros([int(n*(n+1)/2),m,m])
for i in range(0,int(n*(n+1)/2)):
   for j in range(0,m):
	   arr[i,:,j] = np.linspace(0,1,2)

	   
>>> arr
array([[[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]],

       [[0., 0.],
        [1., 1.]]])

With a four-dimensional array you can still do
Python:
arr = np.zeros([n,n,m,m])
for i in range(0,n):
   for j in range(i,n):  # or range(0, i+1) for a lower triangular array
      for k in range(0, m):
         arr[i,j,:,k] = np.linspace(0,1,m)
 
  • Like
Likes schniefen
  • #6
pasmith said:
With a four-dimensional array you can still do
Python:
arr = np.zeros([n,n,m,m])
for i in range(0,n):
   for j in range(i,n):  # or range(0, i+1) for a lower triangular array
      for k in range(0, m):
         arr[i,j,:,k] = np.linspace(0,1,m)
As you comment in the code, this is an upper triangular matrix of matrices. The matrices that only have 0 elements will not be used, but maybe having it this way is more efficient compared to constructing some kind of list of list of matrices with n-1 entries in the first list, n-2 matrices in the second, ...
 

1. What is a 4D array with elements only on one side of the diagonal?

A 4D array with elements only on one side of the diagonal is a multidimensional array with four dimensions where the elements are only present on one side of the diagonal line. This means that the elements are arranged in a specific pattern where they are either above or below the diagonal line, but not on both sides.

2. How is a 4D array with elements only on one side of the diagonal useful?

A 4D array with elements only on one side of the diagonal can be useful in various scientific and mathematical applications. It can help in representing and analyzing complex data structures, such as tensors and matrices, and can also be used in solving differential equations and other mathematical problems.

3. What are some examples of 4D arrays with elements only on one side of the diagonal?

Some examples of 4D arrays with elements only on one side of the diagonal include covariance matrices, correlation matrices, and Jacobian matrices. These arrays are commonly used in statistics, physics, and engineering to represent and analyze data in a four-dimensional space.

4. How is a 4D array with elements only on one side of the diagonal different from a regular 4D array?

The main difference between a 4D array with elements only on one side of the diagonal and a regular 4D array is the arrangement of elements. In a regular 4D array, elements can be present on both sides of the diagonal line, while in a 4D array with elements only on one side of the diagonal, elements are constrained to be either above or below the diagonal line.

5. How can one create and manipulate a 4D array with elements only on one side of the diagonal?

To create and manipulate a 4D array with elements only on one side of the diagonal, one can use programming languages like MATLAB, Python, or R. These languages have built-in functions and libraries that allow for the creation, manipulation, and analysis of multidimensional arrays. Additionally, one can use mathematical operations and algorithms to manipulate the elements of the array to fit the desired pattern.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
2
Views
21K
Replies
6
Views
1K
  • Precalculus Mathematics Homework Help
Replies
32
Views
838
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top