reshape
The reshape operator produces a result array with the same cells as a given array, but a different shape.
Synopsis
reshape(source_array, template_array|schema_definition);
Inputs
- source_array: An existing array in the SciDB system that is to be re-shaped.
- template_array: An existing array in the SciDB system from which the new schema will be retrieved and used to generate the resulting array.
- schema_definition: The schema to be used for the resulting array.
Summary
The reshape operator produces a result array containing the same cells as (but a different shape from) an existing array. The new array requires the same number of attributes as the source array. The reshape operator cannot convert attributes to dimensions or vice versa. For that, use redimension.
The new array requires the same number of cells as the source array, but the resulting array can have more or fewer dimensions than the source.
Consider a 3x4 source array. From a 3x4 source array, reshape can produce a result array of one, two, three, or even more dimensions:Â
- One dimension: the new array's sole dimension has size 12.
- Two dimensions: The new array can be 1x12, 2x6, 3x4, 4x3, 6x2, or 12x1.
- Three dimensions: The new array can be PxQxR, where P, Q, and R are positive integers whose product equals 12, the number of cells in the source array.
- More dimensions: The new array can have any number of dimensions, as long as the product of the dimension sizes equals the number of cells in the source array. Allowable shapes for a 12-cell source array can include 1x1x2x6 and 1x1x12x1x1.Â
To indicate the shape of the result array, either:Â
- Refer to an existing array with the template_array parameter. The new array has the same schema as the template_array. The template_array and the source_array will not be changed by the reshape operator.
- Declare the schema explicitly with the schema_definition parameter. The source_array will not be changed by the reshape operator.
Â
The reshape operator neither alters the source array, nor works for a source array that has a non-zero chunk overlap.
Example
To reshape a 3×4 array into various other 12-cell arrays, do the following:
Create an array called m3x4:
AFL% CREATE ARRAY m3x4 <val:int64>[i=0:2; j=0:3];
The output is:Query was executed successfully
Store values of 1–12 in the array m3x4:
AFL% store(build(m3x4,i*4+j+1),m3x4);Â
The output is:{i,j} val {0,0} 1 {0,1} 2 {0,2} 3 {0,3} 4 {1,0} 5 {1,1} 6 {1,2} 7 {1,3} 8 {2,0} 9 {2,1} 10 {2,2} 11 {2,3} 12
Â
Reshape the m3x4 array as a 6x2 array using the schema_definition method:
AFL% reshape(m3x4,<val:int64>[i=0:5; j=0:1]);Â
The output is:{i,j} val {0,0} 1 {0,1} 2 {1,0} 3 {1,1} 4 {2,0} 5 {2,1} 6 {3,0} 7 {3,1} 8 {4,0} 9 {4,1} 10 {5,0} 11 {5,1} 12
Â
Reshape the m3x4 array as a 2x6 array using the schema_definition method:
AFL% reshape(m3x4,<val:int64>[i=0:1; j=0:5]);Â
The output is:{i,j} val {0,0} 1 {0,1} 2 {0,2} 3 {0,3} 4 {0,4} 5 {0,5} 6 {1,0} 7 {1,1} 8 {1,2} 9 {1,3} 10 {1,4} 11 {1,5} 12
Reshape m3x4 array as 3x2x2 array using the schema_definition method:
AFL% reshape(m3x4,<val:int64>[p=0:2; q=0:1; r=0:1]);Â
The output is:{p,q,r} val {0,0,0} 1 {0,0,1} 2 {0,1,0} 3 {0,1,1} 4 {1,0,0} 5 {1,0,1} 6 {1,1,0} 7 {1,1,1} 8 {2,0,0} 9 {2,0,1} 10 {2,1,0} 11 {2,1,1} 12
Reshape m3x4 array as 12 (a one-dimensional array of size 12) array using the schema_definition method:
AFL% reshape(m3x4,<val:int64>[p=0:11]);Â
The output is:{p} val {0} 1 {1} 2 {2} 3 {3} 4 {4} 5 {5} 6 {6} 7 {7} 8 {8} 9 {9} 10 {10} 11 {11} 12
Â
Reshape m3x4 array as 1x12 (a two-dimensional array where the size of one of the dimensions equals 1) array using the schema_definition method:
AFL% reshape(m3x4,<val:int64>[p=0:0; q=0:11]); Â
The output is:{p,q} val {0,0} 1 {0,1} 2 {0,2} 3 {0,3} 4 {0,4} 5 {0,5} 6 {0,6} 7 {0,7} 8 {0,8} 9 {0,9} 10 {0,10} 11 {0,11} 12
Create a 2x3x2 template_array:
AFL% create array template_m2x3x2 <val:int64>[p=0:1; q=0:2; r=0:1];
The output is:Query was executed successfully
Reshape m3x4 array as 2x3x2 array using the template_array method:
AFL% reshape(m3x4,template_m2x3x2);Â
The output is:{p,q,r} val {0,0,0} 1 {0,0,1} 2 {0,1,0} 3 {0,1,1} 4 {0,2,0} 5 {0,2,1} 6 {1,0,0} 7 {1,0,1} 8 {1,1,0} 9 {1,1,1} 10 {1,2,0} 11 {1,2,1} 12
Remove the template array:
AFL% remove(template_m2x3x2);Â
The output is:Query was executed successfully
Remove the m3x4 array:
AFL% remove(m3x4);Â
The output is:Query was executed successfully