Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Multi-dimensional_array

View file

@ -0,0 +1,20 @@
For the purposes of this task, the actual memory layout or access method of this data structure is not mandated.
It is enough to:
# State the number and extent of each index to the array.
# Provide specific, ordered, integer indices for all dimensions of the array together with a new value to update the indexed value.
# Provide specific, ordered, numeric indices for all dimensions of the array to obtain the arrays value at that indexed position.
;Task:
* State if the language supports multi-dimensional arrays in its syntax and usual implementation.
* State whether the language uses [https://en.wikipedia.org/wiki/Row-major_order row-major or column major order] for multi-dimensional array storage, or any other relevant kind of storage.
* Show how to create a four dimensional array in your language and set, access, set to another value; and access the new value of an integer-indexed item of the array.<br> The idiomatic method for the language is preferred.
:* The array should allow a range of five, four, three and two (or two three four five if convenient), in each of the indices, in order. (For example, ''if'' indexing starts at zero for the first index then a range of 0..4 inclusive would suffice).
* State if memory allocation is optimised for the array - especially if contiguous memory is likely to be allocated.
* If the language has exceptional native multi-dimensional array support such as optional bounds checking, reshaping, or being able to state both the lower and upper bounds of index ranges, then this is the task to mention them.
Show all output here, (but you may judiciously use ellipses to shorten repetitive output text).
<br><br>

View file

@ -0,0 +1,18 @@
V arr = [
[
[1,2,3],
[4,5,6]
],
[
[11,22,33],
[44,55,66]
],
[
[111,222,333],
[444,555,666]
],
[
[1111,2222,3333],
[4444,5555,6666]
]
]

View file

@ -0,0 +1 @@
[ 1 : 5, 1 : 4, 1 : 3, 1 : 2 ]INT a;

View file

@ -0,0 +1 @@
[ 5, 4, 3, 2 ]INT a;

View file

@ -0,0 +1 @@
[ -7 : -3. -3 : 0, -1 : 1, 0 : 1 ]INT x;

View file

@ -0,0 +1 @@
a[ 1, 1, 1, 1 ] := 0;

View file

@ -0,0 +1 @@
a[ 5, 4, 3, 2 ] := a[ 1, 1, 1, 1 ];

View file

@ -0,0 +1 @@
a[ 3 : 4, 1, 1, 1 ] := a[ 5, 4, 2 : 3, 1 ];

View file

@ -0,0 +1,21 @@
# Note this requires the lower bounds of each index be 1 #
a := ( ( ( ( 1111, 1112 ), ( 1121, 1122 ), ( 1131, 1132 ) )
, ( ( 1211, 1212 ), ( 1221, 1222 ), ( 1231, 1232 ) )
, ( ( 1311, 1312 ), ( 1321, 1322 ), ( 1331, 1332 ) )
, ( ( 1411, 1412 ), ( 1421, 1422 ), ( 1431, 1432 ) ) )
, ( ( ( 2111, 2112 ), ( 2121, 2122 ), ( 2131, 2132 ) )
, ( ( 2211, 2212 ), ( 2221, 2222 ), ( 2231, 2232 ) )
, ( ( 2311, 2312 ), ( 2321, 2322 ), ( 2331, 2332 ) )
, ( ( 2411, 2412 ), ( 2421, 2422 ), ( 2431, 2432 ) ) )
, ( ( ( 3111, 3112 ), ( 3121, 3122 ), ( 3131, 3132 ) )
, ( ( 3211, 3212 ), ( 3221, 3222 ), ( 3231, 3232 ) )
, ( ( 3311, 3312 ), ( 3321, 3322 ), ( 3331, 3332 ) )
, ( ( 3411, 3412 ), ( 3421, 3422 ), ( 3431, 3432 ) ) )
, ( ( ( 4111, 4112 ), ( 4121, 4122 ), ( 4131, 4132 ) )
, ( ( 4211, 4212 ), ( 4221, 4222 ), ( 4231, 4232 ) )
, ( ( 4311, 4312 ), ( 4321, 4322 ), ( 4331, 4332 ) )
, ( ( 4411, 4412 ), ( 4421, 4422 ), ( 4431, 4432 ) ) )
, ( ( ( 5111, 5112 ), ( 5121, 5122 ), ( 5131, 5132 ) )
, ( ( 5211, 5212 ), ( 5221, 5222 ), ( 5231, 5232 ) )
, ( ( 5311, 5312 ), ( 5321, 5322 ), ( 5331, 5332 ) )
, ( ( 5411, 5412 ), ( 5421, 5422 ), ( 5431, 5432 ) ) ) );

View file

@ -0,0 +1,25 @@
# declare an array the same size as a, but with all lower bounds equal to 2: #
[ 2 : 6, 2 : 5, 2 : 4, 2 : 3 ]INT b;
# set b to the same values as a above: #
b[ AT 1, AT 1, AT 1, AT 1 ] :=
( ( ( ( 1111, 1112 ), ( 1121, 1122 ), ( 1131, 1132 ) )
, ( ( 1211, 1212 ), ( 1221, 1222 ), ( 1231, 1232 ) )
, ( ( 1311, 1312 ), ( 1321, 1322 ), ( 1331, 1332 ) )
, ( ( 1411, 1412 ), ( 1421, 1422 ), ( 1431, 1432 ) ) )
, ( ( ( 2111, 2112 ), ( 2121, 2122 ), ( 2131, 2132 ) )
, ( ( 2211, 2212 ), ( 2221, 2222 ), ( 2231, 2232 ) )
, ( ( 2311, 2312 ), ( 2321, 2322 ), ( 2331, 2332 ) )
, ( ( 2411, 2412 ), ( 2421, 2422 ), ( 2431, 2432 ) ) )
, ( ( ( 3111, 3112 ), ( 3121, 3122 ), ( 3131, 3132 ) )
, ( ( 3211, 3212 ), ( 3221, 3222 ), ( 3231, 3232 ) )
, ( ( 3311, 3312 ), ( 3321, 3322 ), ( 3331, 3332 ) )
, ( ( 3411, 3412 ), ( 3421, 3422 ), ( 3431, 3432 ) ) )
, ( ( ( 4111, 4112 ), ( 4121, 4122 ), ( 4131, 4132 ) )
, ( ( 4211, 4212 ), ( 4221, 4222 ), ( 4231, 4232 ) )
, ( ( 4311, 4312 ), ( 4321, 4322 ), ( 4331, 4332 ) )
, ( ( 4411, 4412 ), ( 4421, 4422 ), ( 4431, 4432 ) ) )
, ( ( ( 5111, 5112 ), ( 5121, 5122 ), ( 5131, 5132 ) )
, ( ( 5211, 5212 ), ( 5221, 5222 ), ( 5231, 5232 ) )
, ( ( 5311, 5312 ), ( 5321, 5322 ), ( 5331, 5332 ) )
, ( ( 5411, 5412 ), ( 5421, 5422 ), ( 5431, 5432 ) ) ) );

View file

@ -0,0 +1,3 @@
# E.g. the following prints the lower and upper bounds of the first two #
# indexes of a ( in this case, 1, 5, 1 and 4 ) #
print( ( 1 LWB a, 1 UPB a, 2 LWB a, 2 UPB a, newline ) );

View file

@ -0,0 +1 @@
integer array a ( 1 :: 5, 1 :: 4, 1 :: 3, 1 :: 2 );

View file

@ -0,0 +1 @@
integer array x ( -7 :: -3. -3 :: 0, -1 :: 1, 0 :: 1 );

View file

@ -0,0 +1 @@
a( 1, 1, 1, 1 ) := 0;

View file

@ -0,0 +1 @@
a( 5, 4, 3, 2 ) := a( 1, 1, 1, 1 );

View file

@ -0,0 +1,5 @@
% declare a procedure that has a three-dimensional array parameter %
procedure p ( integer array a1 ( *, *, * ) ) ; a1( 1, 2, 1 ) := 3 ;
% call the procedure with a subset of the 4 dimensional array %
p( a( *, 2, *, * ) );

View file

@ -0,0 +1,5 @@
type Grade is range 0..100;
subtype Lower_Case is Character range 'a'..'z';
subtype Natural is Integer range 0..Integer'Last;
subtype Positive is Integer range 1..Integer'Last;
type Deflection is range -180..180;

View file

@ -0,0 +1,4 @@
Mat1 : Matrix;
Mat2 : Matrix;
...
Mat1 := Mat2;

View file

@ -0,0 +1,4 @@
V1 : Vector := (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
V2 : Vector := (2, 2, 2, 2, 2, 2, 2, 2, 2, 2);
...
V2(3..6) := V1(7..10);

View file

@ -0,0 +1,8 @@
function "*" (Left : Vector; Right : Integer) return Vector is
Result : Vector;
begin
for I in Vector'Range loop
Result(I) := Left(I) * Right;
end loop;
return Result;
end "*"

View file

@ -0,0 +1,12 @@
type Unconstrained_Vector is array (Positive range <>) of Integer;
U1 : Unconstrained_Vector := (1,2,3,4,5,6,7,8,9,10);
U2 : Unconstrained_Vector := (10,11,12,13);
...
function "*" (Left : Unconstrained_Vector; Right : Integer) return Unconstrained_Vector is
Result : Unconstrained_Vector(Left'Range);
begin
for I in Left'Range loop
Result(I) := Left(I) * Right;
end loop;
return Result;
end "*";

View file

@ -0,0 +1,2 @@
type Char_Array is array (0..9) of Character; -- A constrained array of characters
type String is array (Positive range <>) of Character; -- An unconstrained array of characters

View file

@ -0,0 +1 @@
subtype Positive is Integer range 1..Integer'Last;

View file

@ -0,0 +1 @@
Name : String := "Rosetta Code";

View file

@ -0,0 +1 @@
Num_Elements : Natural := Char_Array'Length;

View file

@ -0,0 +1,2 @@
type Vector is array (1..10) of Integer;
type Table is array (0..99) of Vector;

View file

@ -0,0 +1,3 @@
V : Vector;
...
V(2) := 10;

View file

@ -0,0 +1,3 @@
T : Table;
...
T(1)(2) := 10;

View file

@ -0,0 +1,4 @@
type Matrix is array (0..99, 1..10) of Integer;
M : Matrix;
...
M(1,2) := 10;

View file

@ -0,0 +1,69 @@
#include <iostream>
#include <vector>
// convienince for printing the contents of a collection
template<typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& c) {
auto it = c.cbegin();
auto end = c.cend();
out << '[';
if (it != end) {
out << *it;
it = std::next(it);
}
while (it != end) {
out << ", " << *it;
it = std::next(it);
}
return out << ']';
}
void fourDim() {
using namespace std;
// create a 4d jagged array, with bounds checking etc...
vector<vector<vector<vector<int>>>> arr;
int cnt = 0;
arr.push_back(vector<vector<vector<int>>>{});
arr[0].push_back(vector<vector<int>>{});
arr[0][0].push_back(vector<int>{});
arr[0][0][0].push_back(cnt++);
arr[0][0][0].push_back(cnt++);
arr[0][0][0].push_back(cnt++);
arr[0][0][0].push_back(cnt++);
arr[0].push_back(vector<vector<int>>{});
arr[0][1].push_back(vector<int>{});
arr[0][1][0].push_back(cnt++);
arr[0][1][0].push_back(cnt++);
arr[0][1][0].push_back(cnt++);
arr[0][1][0].push_back(cnt++);
arr[0][1].push_back(vector<int>{});
arr[0][1][1].push_back(cnt++);
arr[0][1][1].push_back(cnt++);
arr[0][1][1].push_back(cnt++);
arr[0][1][1].push_back(cnt++);
arr.push_back(vector<vector<vector<int>>>{});
arr[1].push_back(vector<vector<int>>{});
arr[1][0].push_back(vector<int>{});
arr[1][0][0].push_back(cnt++);
arr[1][0][0].push_back(cnt++);
arr[1][0][0].push_back(cnt++);
arr[1][0][0].push_back(cnt++);
cout << arr << '\n';
}
int main() {
/* C++ does not have native support for multi-dimensional arrays,
* but classes could be written to make things easier to work with.
* There are standard library classes which can be used for single dimension arrays.
* Also raw access is supported through pointers as in C.
*/
fourDim();
return 0;
}

View file

@ -0,0 +1,9 @@
var array = new int[,] { //Dimensions are inferred
{ 1, 2, 3 },
{ 4, 5, 6}
}
//Accessing a single element:
array[0, 0] = 999;
//To create a 4-dimensional array with all zeroes:
var array = new int[5, 4, 3, 2];

View file

@ -0,0 +1,18 @@
var array = new int[][] { //Dimensions are inferred
new [] { 1, 2, 3, 4 },
new [] { 5, 6, 7, 8, 9, 10 }
}
//Accessing a single element:
array[0][0] = 999;
//To create a 4-dimensional array with all zeroes:
var array = new int[5][][][];
for (int a = 0; a < array.Length; a++) {
array[a] = new int[4][][];
for (int b = 0; b < array[a].Length; b++) {
array[a][b] = new int[3][];
for (int c = 0; c < array[a][b].Length; c++) {
array[a][b][c] = new int[2];
}
}
}

View file

@ -0,0 +1,17 @@
var array = (int[,,,])Array.CreateInstance(typeof(int), new [] { 5, 4, 3, 2 }, new [] { 10, 10, 10, 10 });
int n = 1;
//Note: GetUpperBound is inclusive
for (int a = array.GetLowerBound(0); a <= array.GetUpperBound(0); a++)
for (int b = array.GetLowerBound(1); b <= array.GetUpperBound(1); b++)
for (int c = array.GetLowerBound(2); c <= array.GetUpperBound(2); c++)
for (int d = array.GetLowerBound(3); d <= array.GetUpperBound(3); d++)
array[a, b, c, d] = n++;
//To set the first value, we must now use the lower bounds:
array[10, 10, 10, 10] = 999;
//As with all arrays, Length gives the TOTAL length.
Console.WriteLine("Length: " + array.Length);
Console.WriteLine("First 30 elements:");
//The multidimensional array does not implement the generic IEnumerable<int>
//so we need to cast the elements.
Console.WriteLine(string.Join(" ", array.Cast<int>().Take(30)) + " ...");

View file

@ -0,0 +1,37 @@
/*Single dimensional array of integers*/
int a[10];
/*2-dimensional array, also called matrix of floating point numbers.
This matrix has 3 rows and 2 columns.*/
float b[3][2];
/*3-dimensional array ( Cube ? Cuboid ? Lattice ?) of characters*/
char c[4][5][6];
/*4-dimensional array (Hypercube ?) of doubles*/
double d[6][7][8][9];
/*Note that the right most number in the [] is required, all the others may be omitted.
Thus this is ok : */
int e[][3];
/*But this is not*/
float f[5][4][];
/*But why bother with all those numbers ? You can also write :*/
int *g;
/*And for a matrix*/
float **h;
/*or if you want to show off*/
double **i[];
/*you get the idea*/
char **j[][5];

View file

@ -0,0 +1,27 @@
#include<stdio.h>
int main()
{
int hyperCube[5][4][3][2];
/*An element is set*/
hyperCube[4][3][2][1] = 1;
/*IMPORTANT : C ( and hence C++ and Java and everyone of the family ) arrays are zero based.
The above element is thus actually the last element of the hypercube.*/
/*Now we print out that element*/
printf("\n%d",hyperCube[4][3][2][1]);
/*But that's not the only way to get at that element*/
printf("\n%d",*(*(*(*(hyperCube + 4) + 3) + 2) + 1));
/*Yes, I know, it's beautiful*/
*(*(*(*(hyperCube+3)+2)+1)) = 3;
printf("\n%d",hyperCube[3][2][1][0]);
return 0;
}

View file

@ -0,0 +1,66 @@
#include<stdlib.h>
#include<stdio.h>
/*The stdlib header file is required for the malloc and free functions*/
int main()
{
/*Declaring a four fold integer pointer, also called
a pointer to a pointer to a pointer to an integer pointer*/
int**** hyperCube, i,j,k;
/*We will need i,j,k for the memory allocation*/
/*First the five lines*/
hyperCube = (int****)malloc(5*sizeof(int***));
/*Now the four planes*/
for(i=0;i<5;i++){
hyperCube[i] = (int***)malloc(4*sizeof(int**));
/*Now the 3 cubes*/
for(j=0;j<4;j++){
hyperCube[i][j] = (int**)malloc(3*sizeof(int*));
/*Now the 2 hypercubes (?)*/
for(k=0;k<3;k++){
hyperCube[i][j][k] = (int*)malloc(2*sizeof(int));
}
}
}
/*All that looping and function calls may seem futile now,
but imagine real applications when the dimensions of the dataset are
not known beforehand*/
/*Yes, I just copied the rest from the first program*/
hyperCube[4][3][2][1] = 1;
/*IMPORTANT : C ( and hence C++ and Java and everyone of the family ) arrays are zero based.
The above element is thus actually the last element of the hypercube.*/
/*Now we print out that element*/
printf("\n%d",hyperCube[4][3][2][1]);
/*But that's not the only way to get at that element*/
printf("\n%d",*(*(*(*(hyperCube + 4) + 3) + 2) + 1));
/*Yes, I know, it's beautiful*/
*(*(*(*(hyperCube+3)+2)+1)) = 3;
printf("\n%d",hyperCube[3][2][1][0]);
/*Always nice to clean up after you, yes memory is cheap, but C is 45+ years old,
and anyways, imagine you are dealing with terabytes of data, or more...*/
free(hyperCube);
return 0;
}

View file

@ -0,0 +1,161 @@
import std.stdio;
/*
* Using just what is built-in, D only supports single dimension arrays. Like other languages, arrays can be built up as jagged arrays.
* Arrays can either be created with a fixed size, or dynamically with support for resizing.
*/
void nativeExample() {
int[3] staticArray; // Statically allocated array. Will only contain three elements, accessed from 0 to 2 inclusive.
staticArray[0] = 1;
staticArray[1] = 2;
staticArray[2] = 3;
writeln("Static array: ", staticArray);
int[] dynamicArray; // Dynamically allocated array.
dynamicArray.length = 3; // The array can be resized at runtime. If the number elements exceeds the allocated memory, new memory will be given from the heap.
dynamicArray[0] = 4;
dynamicArray[1] = 5;
dynamicArray[2] = 6;
dynamicArray ~= 7; // New elements can be concatenated.
writeln("Dynamic array: ", dynamicArray);
}
/*
* Multi-dimensional arrays can be created as custom types (classes or structs). They can have as many dimensions as are written for support.
* The indexes can either be standard 0-n, or arbitrary m-n as needed. This example shows just a two dimensional example with standard indexes.
* As few or as many of these pieces can be implemented as desired (compile-time error is given if a feature is not supported).
*/
struct Matrix(T) {
// A dynamic array for the actual storage.
private:
T[] source;
uint rows, cols; // dimensions
public:
this(uint m, uint n) {
rows = m;
cols = n;
source.length = m*n;
}
/// Allow for short access to limits, e.g. a[$-1,$-1]
int opDollar(size_t pos : 0)() const {
return rows;
}
int opDollar(size_t pos : 1)() const {
return cols;
}
/// Allow for indexing to read a value, e.g. a[0,0]
T opIndex(int i, int j) const in {
assert(0 <= i && i <= rows, "Row index out of bounds");
assert(0 <= j && j <= cols, "Col index out of bounds");
} body {
return source[i*rows + j];
}
/// Allow for assigning elements, e.g. a[0,0] = c
T opIndexAssign(T elem, int i, int j) in {
assert(0 <= i && i <= rows, "Row index out of bounds");
assert(0 <= j && j <= cols, "Col index out of bounds");
} body {
auto index = rows*i + j;
T prev = source[index];
source[index] = elem;
return prev;
}
/// Allow for applying operations and assigning elements, e.g. a[0,0] += c
T opIndexOpAssign(string op)(T elem, int i, int j) {
auto index = rows*i + j;
T prev = source[index];
mixin("source[index] " ~ op ~ "= elem;");
return prev;
}
/// Support slicing, shown below
auto opSlice(size_t pos)(int a, int b) in {
assert(0 <= a && a <= opDollar!pos);
assert(0 <= b && b <= opDollar!pos);
} body {
if (pos == 0) {
} else {
assert(0 <= a && a <= cols, "Col slice out of bounds");
assert(0 <= b && b <= cols, "Col slice out of bounds");
}
return [a, b];
}
/// Allow for getting a sub-portion of the matrix, e.g. [0..2, 2..4]
auto opIndex(int[] a, int[] b) const {
auto t = Matrix!T(a.length, b.length);
foreach(i, ia; a) {
foreach(j, jb; b) {
t[i, j] = this[ia, jb];
}
}
return t;
}
auto opIndex(int[] a, int b) const {
return opIndex(a, [b,b+1]);
}
auto opIndex(int a, int[] b) const {
return opIndex([a,a+1], b);
}
/// Assign a single value to every element
void opAssign(T value) {
source[0..$] = value;
}
/// Assign a single element to a subset of the Matrix
void opIndexAssign(T elem, int[] a, int[] b) {
for (int i=a[0]; i<a[1]; i++) {
auto start = rows * i;
source[start+b[0]..start+b[1]] = elem;
}
}
void opIndexAssign(T elem, int[] a, int b) {
opIndexAssign(elem, a, [b, b+1]);
}
void opIndexAssign(T elem, int a, int[] b) {
opIndexAssign(elem, [a, a+1], b);
}
/// Define how to write Matrix values as a string. Only does a simple string representation
void toString(scope void delegate(const(char)[]) sink) const {
import std.format;
sink("[");
foreach (i; 0..opDollar!0) {
sink("[");
foreach (j; 0..opDollar!1) {
formattedWrite(sink, "%s", opIndex(i,j));
if (j < cols-1) sink(", ");
}
if (i < rows-1) sink("]\n ");
else sink("]");
}
sink("]");
}
}
void customArray() {
auto multi = Matrix!int(3, 3);
writeln("Create a multidimensional object:\n", multi);
writeln("Access an element: ", multi[0,0]);
multi[0,0] = 1;
writeln("Assign an element: ", multi[0,0]);
multi[0,0] += 2;
writeln("Arithmetic on an element: ", multi[0,0]);
writeln("Slice a matrix:\n", multi[0..2, 0..2]);
multi = 5;
writeln("Assign all elements:\n", multi);
multi[0..2,1..3] = 4;
writeln("Assign some elements:\n", multi);
}
void main() {
nativeExample();
customArray();
}

View file

@ -0,0 +1,31 @@
{1. Delphi support arrays of any number of dimensions, including non-orthogonal arrays.}
{2. Delphi arrays are Row Major.}
{3. This creates a five dimensional array in Delphi.}
var MyArray: array [0..5,0..4,0..3,0..2] of integer;
{4. This reads and writes array items:}
MyArray[5,4,3,2]:=MyArray[1,2,3,2];
{5. Array memory space can be compacted to guarantee that all items are contiguous.}
var MyArray3: packed array [0..5,0..4,0..3,0..2] of integer;
{6. You can create array that start at indices other than zero.}
var MyArray2: array [5..25,26..50] of integer;
{7. You can create non-orthogonal arrays:}
var A : array of array of string;
var I, J : Integer;
begin
SetLength(A, 10);
for I := Low(A) to High(A) do
begin
SetLength(A[I], I);
for J := Low(A[I]) to High(A[I]) do
A[I,J] := IntToStr(I) + ',' + IntToStr(J) + ' ';
end;
end;

View file

@ -0,0 +1,45 @@
(require 'math) ;; dot-product
;; dims = vector #(d1 d2 .....)
;; allocates a new m-array
(define (make-m-array dims (init 0))
;; allocate 2 + d1*d2*d3... consecutive cells
(define msize (apply * (vector->list dims)))
(define m-array (make-vector (+ 2 msize) init))
;; compute displacements vector once for all
;; m-array[0] = [1 d1 d1*d2 d1*d2*d3 ...]
(define disps (vector-rotate! (vector-dup dims) 1))
(vector-set! disps 0 1)
(for [(i(in-range 1 (vector-length disps)) )]
(vector-set! disps i (* [disps i] [disps (1- i)])))
(vector-set! m-array 0 disps)
(vector-set! m-array 1 dims) ;; remember dims
m-array)
;; from indices = #(i j k ...) to displacement
(define-syntax-rule (m-array-index ma indices)
(+ 2 (dot-product (ma 0) indices)))
;; check i < d1, j < d2, ...
(define (m-array-check ma indices)
(for [(dim [ma 1]) (idx indices)]
#:break (>= idx dim) => (error 'm-array:bad-index (list idx '>= dim))))
;; --------------------
;; A P I
;; --------------------
;; indices is a vector #[i j k ...]
;; (make-m-array (dims) [init])
(define (m-array-dims ma) [ma 1])
; return ma[indices]
(define (m-array-ref ma indices)
(m-array-check ma indices)
[ma (m-array-index ma indices)])
; sets ma[indices]
(define (m-array-set! ma indices value )
(m-array-check ma indices)
(vector-set! ma (m-array-index ma indices) value))

View file

@ -0,0 +1,19 @@
USING: accessors arrays.shaped io kernel prettyprint sequences ;
! Create a 4-dimensional array with increasing elements.
{ 2 3 4 5 } increasing
! Check if an index is in bounds.
{ 0 0 0 0 } over shaped-bounds-check
! Access and print the first element.
"First element: " write
get-shaped-row-major .
! Set the first element and show the array.
"With first element set to 999:" print
999 { 0 0 0 0 } pick set-shaped-row-major dup .
! Reshape and show the array.
"Reshaped: " print
{ 5 4 3 2 } reshape .

View file

@ -0,0 +1,5 @@
4 5 6 7 8 CELL 5 darray test5d \ Creates the array
i j k l m test5d \ Returns the address of test5d[ i, j, k, l, m ]
100 i j k l m test5d ! \ Sets contents of test5d[ i, j, k, l, m ] to 100
i j k l m test5d @ \ Gets contents of test5d[ i, j, k, l, m ]

View file

@ -0,0 +1,74 @@
\ Values used to avoid locals or some confusing stack juggling.
\ They could be replaced by locals if preferred.
0 VALUE addr
0 VALUE ndims
: darray \ Create: d(n-1) .. d0 size n "<name-of-array>" -- ;
\ Does: ix[n-1] .. ix0 -- addr
\ Creates an n dimensional array with axes dn-1 .. d0
\ d(n-i) .. d0 - the length of the n dimensions.
\ size - The size in bytes of the basic elenment in the array.
\ - e.g 1 for chars, 2 for 16 bit etc..
\ n - The number of dimensions
\ Term stride is taken from numpy. It's the gap between consqutive indices
\ in a given dimension. ix * stride gives an offset from a base address.
\ CELLS 0 1 2 ... n+1 n+2 ->
\ Stride[0] addr Stride[n] addr stride[0] stride[n-1] data ->
\ CELL[2] CELL[n+2] size
\ Storing the stride addresses at CELLS 0 and 1 gives fast access for
\ the do loop in the DOES> section to loop through the strides.
CREATE
IS ndims HERE IS addr \ n to ndims, HERE to addr
0 , 0 , \ Placeholders for loop-bounds
DUP , \ size stored at CELL[2]
ndims 1- 0 ?DO * DUP , LOOP \ Calculate and store strides 1 to n-1.
HERE addr CELL + ! \ Store high address for strides
* ALLOT \ Calculate and allocate the space
addr 2 CELLS + addr ! \ Calculate & store low addr for strides
DOES> \ ix[n-1] .. ix[0] -- addr ;
2@ OVER SWAP ( data-addr stride-addr[n] stride-addr[0] )
?DO
SWAP i @ * + \ multiply strides by indices and add to base address
CELL +LOOP ;
2 3 4 5 CELL 4 darray test4d \ 4d array of integers
\ 2 3 4 5 20 4 darray 20byte-records-4D
\ Word to fill the array
: test4d-fill
2 0 DO i
3 0 DO i
4 0 DO
5 0 DO
2DUP 100 * SWAP 1000 * +
j 10 * + i +
2 PICK 2 PICK j i test4d !
LOOP
LOOP
DROP LOOP
DROP LOOP ;
: [. ." [ " ;
: ]. ." ] " ;
defer p-array
: print-4d \ <array-name> -- ; Prints 4d array
' IS p-array
2 0 DO i CR [.
3 0 DO i CR 2 SPACES [.
4 0 DO
CR 4 SPACES [.
5 0 DO
2DUP j i p-array @ 8 .r
LOOP ].
LOOP ].
DROP LOOP ].
DROP LOOP ;
test4d-fill
print-4d test4d

View file

@ -0,0 +1,4 @@
-4567 1 2 3 3 test4d ! ok
1 2 3 3 test4d @ . -4567 ok
print-4d test4d

View file

@ -0,0 +1,3 @@
DIMENSION A(5,4,3,2) !Declares a (real) array A of four dimensions, storage permitting.
X = 3*A(2,I,1,K) !Extracts a certain element, multiplies its value by three, result to X.
A(1,2,3,4) = X + 1 !Places a value (the result of the expression X + 1) ... somewhere...

View file

@ -0,0 +1,14 @@
INTEGER A(3,3) !An array, regarded as being a matrix.
DATA A/1,2,3, !Some initial values.
2 4,5,6, !Laid out as if a matrix.
3 7,8,9/ !But supplied as consecutive elements.
WRITE (6,*) "Writing A..."
WRITE (6,1) A !Using the array order.
1 FORMAT (3I2) !After three values, start a new line.
WRITE (6,*) "Writing A(row,col) down rows and across columns..."
DO I = 1,3 !Write each row, one after the other.
WRITE (6,1) (A(I,J), J = 1,3) !The columns along a row.
END DO !Onto the next row.
END

View file

@ -0,0 +1,7 @@
'' declare a three-dimensional array of single
'' precision floating-point numbers.
Dim array(1 To 2, 6, 3 To 5) As Single
'' The first dimension of the declared array
'' has indices from 1 to 2, the second, 0 to 6,
'' and the third, 3 to 5.

View file

@ -0,0 +1,2 @@
' Take Care while initializing multi-dimensional array
Dim As Integer multidim(1 To 2, 1 To 5) = {{0,0,0,0,0}, {0,0,0,0,0}}

View file

@ -0,0 +1,4 @@
Dim As Integer A(5,4,3,2)
A(3,1,0,1) = 3100
A(3,1,0,1) = A(3,1,0,1)+1
Print A(3,1,0,1)

View file

@ -0,0 +1,56 @@
package main
import "fmt"
type md struct {
dim []int
ele []float64
}
func newMD(dim ...int) *md {
n := 1
for _, d := range dim {
n *= d
}
return &md{append([]int{}, dim...), make([]float64, n)}
}
func (m *md) index(i ...int) (x int) {
for d, dx := range m.dim {
x = x*dx + i[d]
}
return
}
func (m *md) at(i ...int) float64 {
return m.ele[m.index(i...)]
}
func (m *md) set(x float64, i ...int) {
m.ele[m.index(i...)] = x
}
func (m *md) show(i ...int) {
fmt.Printf("m%d = %g\n", i, m.at(i...))
}
func main() {
m := newMD(5, 4, 3, 2)
m.show(4, 3, 2, 1)
m.set(87, 4, 3, 2, 1)
m.show(4, 3, 2, 1)
for i := 0; i < m.dim[0]; i++ {
for j := 0; j < m.dim[1]; j++ {
for k := 0; k < m.dim[2]; k++ {
for l := 0; l < m.dim[3]; l++ {
x := m.index(i, j, k, l)
m.set(float64(x)+.1, i, j, k, l)
}
}
}
}
fmt.Println(m.ele[:10])
fmt.Println(m.ele[len(m.ele)-10:])
m.show(4, 3, 2, 1)
}

View file

@ -0,0 +1 @@
A1=:5 4 3 2$0

View file

@ -0,0 +1 @@
A2=:5 4 $ 1 3 2$ 0

View file

@ -0,0 +1,31 @@
i.2 3 4 5
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
35 36 37 38 39
40 41 42 43 44
45 46 47 48 49
50 51 52 53 54
55 56 57 58 59
60 61 62 63 64
65 66 67 68 69
70 71 72 73 74
75 76 77 78 79
80 81 82 83 84
85 86 87 88 89
90 91 92 93 94
95 96 97 98 99
100 101 102 103 104
105 106 107 108 109
110 111 112 113 114
115 116 117 118 119

View file

@ -0,0 +1,2 @@
2 3 4 5#:118
1 2 3 3

View file

@ -0,0 +1,2 @@
(<1 2 3 3) { i.2 3 4 5
118

View file

@ -0,0 +1 @@
A3=: 987 (<1 2 3 3)} i. 2 3 4 5

View file

@ -0,0 +1,5 @@
(<1 2){A3
100 101 102 103 104
105 106 107 108 109
110 111 112 113 114
115 116 117 987 119

View file

@ -0,0 +1,2 @@
(<5 5 5 5){A3
|index error

View file

@ -0,0 +1,2 @@
A1+A3
|length error

View file

@ -0,0 +1,30 @@
public class MultiDimensionalArray {
public static void main(String[] args) {
// create a regular 4 dimensional array and initialize successive elements to the values 1 to 120
int m = 1;
int[][][][] a4 = new int[5][4][3][2];
for (int i = 0; i < a4.length; ++i) {
for (int j = 0; j < a4[0].length; ++j) {
for (int k = 0; k < a4[0][0].length; ++k) {
for (int l = 0; l < a4[0][0][0].length; ++l) {
a4[i][j][k][l] = m++;
}
}
}
}
System.out.println("First element = " + a4[0][0][0][0]); // access and print value of first element
a4[0][0][0][0] = 121; // change value of first element
System.out.println();
for (int i = 0; i < a4.length; ++i) {
for (int j = 0; j < a4[0].length; ++j) {
for (int k = 0; k < a4[0][0].length; ++k) {
for (int l = 0; l < a4[0][0][0].length; ++l) {
System.out.printf("%4d", a4[i][j][k][l]);
}
}
}
}
}
}

View file

@ -0,0 +1,7 @@
function array() {
var dimensions= Array.prototype.slice.call(arguments);
var N=1, rank= dimensions.length;
for (var j= 0; j<rank; j++) N*= dimensions[j];
this.dimensions= dimensions;
this.values= new Array(N);
}

View file

@ -0,0 +1,17 @@
function tobase(base, vals) {
var r= 0, len= base.length;
for (j= 0; j < len; j++) {
r*= base[j];
r+= vals[j];
}
return r;
}
function frombase(base, val) {
var r= new Array(base.length);
for (j= base.length-1; j>= 0; j--) {
r[j]= val%base[j];
val= (val-r[j])/base[j];
}
return r;
}

View file

@ -0,0 +1,4 @@
array.prototype.index= function() {
var indices= Array.prototype.slice.call(arguments);
return this.values[tobase(this.dimensions, indices)];
}

View file

@ -0,0 +1,2 @@
a= new array(6,7,8,9);
a.index(2,3,5,6);

View file

@ -0,0 +1,9 @@
function array(length) {
var rest= Array.prototype.slice.call(arguments);
var r= new Array(length);
if (0<rest.length) {
for (var j= 0; j<length; j++) {
r[j]= array.apply(rest);
}
}
}

View file

@ -0,0 +1,7 @@
# The input is used to initialize the elements of the
# multi-dimensional array:
def multiarray(d):
. as $in
| if (d|length) == 1 then [range(0;d[0]) | $in]
else multiarray(d[1:]) | multiarray( d[0:1] )
end;

View file

@ -0,0 +1 @@
def ary: 0 | multiarray( [5, 4, 3, 2] );

View file

@ -0,0 +1 @@
ary | .[4][3][2][1]

View file

@ -0,0 +1 @@
ary | getpath( [4,3,2,1])

View file

@ -0,0 +1 @@
def ix: [4,3,2,1];

View file

@ -0,0 +1,5 @@
ary | setpath(ix; 100) | getpath(ix)
#=> 100
ary | setpath(ix; 100) | .[4][3][2][1]
#=> 100

View file

@ -0,0 +1,13 @@
def dimensions:
def same(f):
if length == 0 then true
else (.[0]|f) as $first | reduce .[] as $i (true; if . then ($i|f) == $first else . end)
end;
if type == "array"
then if length == 0 then [0]
elif same( dimensions ) then [length] + (.[0]|dimensions)
else null
end
else []
end;

View file

@ -0,0 +1,2 @@
ary | dimensions
#=> [5,4,3,2]

View file

@ -0,0 +1,93 @@
julia> a4d = rand(Int, 5, 4, 3, 2)
5×4×3×2 Array{Int64,4}:
[:, :, 1, 1] =
-4071410509370082480 -361121233222804742 6884099527706004770 -3207257047234122321
8613938183523990915 -6284413064884272355 2092274063225757339 2883192477194384468
8063489472918692884 7817079491035828713 4371491775271354348 276862286105940437
-1772662642291072402 4809985701129914416 8281858591045636672 8406920023376702651
5149318914785292575 -1515076735924485336 -3446939844768424035 1325012392213218275
[:, :, 2, 1] =
6880114761106392524 6088903043766771621 3427296371455723937 -3871156425725741320
-9056731070343072262 -6900689642579036552 6428097039068264082 -8965404397337530418
-1972367399165031500 -2213273885576423725 -5169665910043775523 4625836493333597033
-2599106204536686594 8110110151332124377 2213007499408257641 -8439992945948333993
-5582801554832553928 7198533994867969144 -5086532281938936871 -5785587643130395544
[:, :, 3, 1] =
-17294958981345227 -3894742251505912349 5910938929594406050 -1362996293154965701
3987219021607448038 6324367415515825846 3745581879541731998 2844758786713062315
-6091449020940608221 4456121461951632195 -2584728255467516797 -3497659495227813242
-5928873932509420551 -3918487907573141316 5830965509944713914 8236501134345492283
-8221039525025311485 -1377489816166018715 3331466694873878620 -6251825104964406539
[:, :, 1, 2] =
-3457374235750853577 5435555757951424162 -2045941319469405317 7328458353379957791
1713055171323579940 7991986037097235116 -7241088987591638401 1660634030535548686
4673394669827656364 5361350944786403116 -3165400775730699962 -2786870864776919360
-936605780936708362 -8663743677584705168 2854140834792323092 -8335310793071345837
-3681644266158007140 -725263380984390472 -3882196050441743539 3104333567303051945
[:, :, 2, 2] =
6571168566735939900 -2962119128748096 -6995171731724009568 -3311757615633004375
-2337587211780934167 -4326286389873661148 4350714846343974202 7735721289399158250
-8213453299747381553 -8821356424402127712 -6240807297203610060 -4705744555934991961
5320110310888142259 -767643622294485330 -3059460131766056283 -7562911883833519677
-7578327835336665804 -1125154035165505958 7801099686760373595 -7212481716316687824
[:, :, 3, 2] =
4758459841719113041 -2608915613406792656 -4400228941420712011 8931157241145543293
7349481815101847836 -1609425280933983575 -4442082290554782826 -7044337833155803104
-4991211814494456720 6358355341301107435 -7486441622913196485 -1654445042306345503
-2789599665862904090 2753632931032283690 -1580743162155175963 -5070035295183713618
6026427076366641381 -2363816652576128818 -7282095369321808360 9097339410999816372
julia> a4d[1,1,1,1] = 10
10
julia> a4d
5×4×3×2 Array{Int64,4}:
[:, :, 1, 1] =
10 -361121233222804742 6884099527706004770 -3207257047234122321
8613938183523990915 -6284413064884272355 2092274063225757339 2883192477194384468
8063489472918692884 7817079491035828713 4371491775271354348 276862286105940437
-1772662642291072402 4809985701129914416 8281858591045636672 8406920023376702651
5149318914785292575 -1515076735924485336 -3446939844768424035 1325012392213218275
[:, :, 2, 1] =
6880114761106392524 6088903043766771621 3427296371455723937 -3871156425725741320
-9056731070343072262 -6900689642579036552 6428097039068264082 -8965404397337530418
-1972367399165031500 -2213273885576423725 -5169665910043775523 4625836493333597033
-2599106204536686594 8110110151332124377 2213007499408257641 -8439992945948333993
-5582801554832553928 7198533994867969144 -5086532281938936871 -5785587643130395544
[:, :, 3, 1] =
-17294958981345227 -3894742251505912349 5910938929594406050 -1362996293154965701
3987219021607448038 6324367415515825846 3745581879541731998 2844758786713062315
-6091449020940608221 4456121461951632195 -2584728255467516797 -3497659495227813242
-5928873932509420551 -3918487907573141316 5830965509944713914 8236501134345492283
-8221039525025311485 -1377489816166018715 3331466694873878620 -6251825104964406539
[:, :, 1, 2] =
-3457374235750853577 5435555757951424162 -2045941319469405317 7328458353379957791
1713055171323579940 7991986037097235116 -7241088987591638401 1660634030535548686
4673394669827656364 5361350944786403116 -3165400775730699962 -2786870864776919360
-936605780936708362 -8663743677584705168 2854140834792323092 -8335310793071345837
-3681644266158007140 -725263380984390472 -3882196050441743539 3104333567303051945
[:, :, 2, 2] =
6571168566735939900 -2962119128748096 -6995171731724009568 -3311757615633004375
-2337587211780934167 -4326286389873661148 4350714846343974202 7735721289399158250
-8213453299747381553 -8821356424402127712 -6240807297203610060 -4705744555934991961
5320110310888142259 -767643622294485330 -3059460131766056283 -7562911883833519677
-7578327835336665804 -1125154035165505958 7801099686760373595 -7212481716316687824
[:, :, 3, 2] =
4758459841719113041 -2608915613406792656 -4400228941420712011 8931157241145543293
7349481815101847836 -1609425280933983575 -4442082290554782826 -7044337833155803104
-4991211814494456720 6358355341301107435 -7486441622913196485 -1654445042306345503
-2789599665862904090 2753632931032283690 -1580743162155175963 -5070035295183713618
6026427076366641381 -2363816652576128818 -7282095369321808360 9097339410999816372
julia> a4d[4,3,2,1]
2213007499408257641

View file

@ -0,0 +1,20 @@
include ..\Utilitys.tlhy
0 ( 4 3 2 ) dim pstack
5432 ( 4 3 2 ) set pstack
( 4 3 2 ) get ?
( 4 3 ) get %row !row
$row ?
$row 1 2 set !row
$row ?
$row ( 4 3 ) set
0 4 repeat ( 4 2 ) set
pstack
"End " input

View file

@ -0,0 +1,26 @@
// version 1.1.2
fun main(args: Array<String>) {
// create a regular 4 dimensional array and initialize successive elements to the values 1 to 120
var m = 1
val a4 = Array<Array<Array<Array<Int>>>>(5) {
Array<Array<Array<Int>>>(4) {
Array<Array<Int>>(3) {
Array<Int>(2) { m++ }
}
}
}
println("First element = ${a4[0][0][0][0]}") // access and print value of first element
a4[0][0][0][0] = 121 // change value of first element
println()
// access and print values of all elements
val f = "%4d"
for (i in 0..4)
for (j in 0..3)
for (k in 0..2)
for (l in 0..1)
print(f.format(a4[i][j][k][l]))
}

View file

@ -0,0 +1,14 @@
on array ()
cnt = paramCount()
if cnt=0 then return
arr = []
arr[param(cnt)] = 0
repeat with d = cnt-1 down to 1
newArr = []
repeat with i = 1 to param(d)
newArr[i] = arr.duplicate() -- duplicate does a deep copy
end repeat
arr = newArr.duplicate()
end repeat
return arr
end

View file

@ -0,0 +1,10 @@
-- creates 4-dimensional array with specified ranges (data defaults to 0)
a = array(2, 3, 4, 5)
put a[1][2][3][4]
-- 0
a[1][2][3][4] = 23
put a[1][2][3][4]
-- 23
a[1][2][3][4] = 42
put a[1][2][3][4]
-- 42

View file

@ -0,0 +1,42 @@
-- Variadic, first argument is the value with which to populate the array.
function multiArray (initVal, ...)
local function copy (t)
local new = {}
for k, v in pairs(t) do
if type(v) == "table" then
new[k] = copy(v)
else
new[k] = v
end
end
return new
end
local dimensions, arr, newArr = {...}, {}
for i = 1, dimensions[#dimensions] do table.insert(arr, initVal) end
for d = #dimensions - 1, 1, -1 do
newArr = {}
for i = 1, dimensions[d] do table.insert(newArr, copy(arr)) end
arr = copy(newArr)
end
return arr
end
-- Function to print out the specific example created here
function show4dArray (a)
print("\nPrinting 4D array in 2D...")
for k, v in ipairs(a) do
print(k)
for l, w in ipairs(v) do
print("\t" .. l)
for m, x in ipairs(w) do
print("\t", m, unpack(x))
end
end
end
end
-- Main procedure
local t = multiArray("a", 2, 3, 4, 5)
show4dArray(t)
t[1][1][1][1] = true
show4dArray(t)

View file

@ -0,0 +1,6 @@
a = ConstantArray[0, {2, 3, 4, 5}]
a[[1, 2, 3, 4]] = 15;
a[[1, 2, 3, 4]]
a
a[[2, 2]] = 8;
a

View file

@ -0,0 +1,50 @@
import arraymancer
let c = [
[
[1,2,3],
[4,5,6]
],
[
[11,22,33],
[44,55,66]
],
[
[111,222,333],
[444,555,666]
],
[
[1111,2222,3333],
[4444,5555,6666]
]
].toTensor()
echo c
# Tensor of shape 4x2x3 of type "int" on backend "Cpu"
# | 1 2 3 | 11 22 33 | 111 222 333 | 1111 2222 3333|
# | 4 5 6 | 44 55 66 | 444 555 666 | 4444 5555 6666|
let e = newTensor[bool]([2, 3])
echo e
# Tensor of shape 2x3 of type "bool" on backend "Cpu"
# |false false false|
# |false false false|
let f = zeros[float]([4, 3])
echo f
# Tensor of shape 4x3 of type "float" on backend "Cpu"
# |0.0 0.0 0.0|
# |0.0 0.0 0.0|
# |0.0 0.0 0.0|
# |0.0 0.0 0.0|
let g = ones[float]([4, 3])
echo g
# Tensor of shape 4x3 of type "float" on backend "Cpu"
# |1.0 1.0 1.0|
# |1.0 1.0 1.0|
# |1.0 1.0 1.0|
# |1.0 1.0 1.0|

View file

@ -0,0 +1,31 @@
class MultiArray {
function : Main(args : String[]) ~ Nil {
a4 := Int->New[5,4,3,2];
a4_size := a4->Size();
m := 1;
for(i := 0; i < a4_size[0]; i += 1;) {
for(j := 0; j < a4_size[1]; j += 1;) {
for(k := 0; k < a4_size[2]; k += 1;) {
for(l := 0; l < a4_size[3]; l += 1;) {
a4[i,j,k,l] := m++;
};
};
};
};
System.IO.Standard->Print("First element = ")->PrintLine(a4[0,0,0,0]);
a4[0,0,0,0] := 121;
for(i := 0; i < a4_size[0]; i += 1;) {
for(j := 0; j < a4_size[1]; j += 1;) {
for(k := 0; k < a4_size[2]; k += 1;) {
for(l := 0; l < a4_size[3]; l += 1;) {
System.IO.Standard->Print(a4[i,j,k,l])->Print(" ");
};
};
};
};
""->PrintLine();
}
}

View file

@ -0,0 +1,48 @@
use feature 'say';
# Perl arrays are internally always one-dimensional, but multi-dimension arrays are supported via references.
# So a two-dimensional array is an arrays-of-arrays, (with 'rows' that are references to arrays), while a
# three-dimensional array is an array of arrays-of-arrays, and so on. There are no arbitrary limits on the
# sizes or number of dimensions (i.e. the 'depth' of nesting references).
# To generate a zero-initialized 2x3x4x5 array
for $i (0..1) {
for $j (0..2) {
for $k (0..3) {
$a[$i][$j][$k][$l] = [(0)x5];
}
}
}
# There is no requirement that the overall shape of array be regular, or that contents of
# the array elements be of the the same type. Arrays can contain almost any type of values
@b = (
[1, 2, 4, 8, 16, 32], # numbers
[<Mon Tue Wed Thu Fri Sat Sun>], # strings
[sub{$_[0]+$_[1]}, sub{$_[0]-$_[1]}, sub{$_[0]*$_[1]}, sub{$_[0]/$_[1]}] # coderefs
);
say $b[0][5]; # prints '32'
say $b[1][2]; # prints 'Wed'
say $b[2][0]->(40,2); # prints '42', sum of 40 and 2
# Pre-allocation is possible, can result in a more efficient memory layout
# (in general though Perl allows minimal control over memory)
$#$big = 1_000_000;
# But dimensions do not need to be pre-declared or pre-allocated.
# Perl will auto-vivify the necessary storage slots on first access.
$c[2][2] = 42;
# @c =
# [undef]
# [undef]
# [undef, undef, 42]
# Negative indicies to count backwards from the end of each dimension
say $c[-1][-1]; # prints '42'
# Elements of an array can be set one-at-a-time or in groups via slices
my @d = <Mon Tue Ned Sat Fri Thu>;
push @d, 'Sun';
$d[2] = 'Wed';
@d[3..5] = @d[reverse 3..5];
say "@d"; # prints 'Mon Tue Wed Thu Fri Sat Sun'

View file

@ -0,0 +1,3 @@
-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">xyz</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">),</span><span style="color: #000000;">y</span><span style="color: #0000FF;">),</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,5 @@
-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">plane</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">space</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plane</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,13 @@
-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rqd</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #000000;">4</span><span style="color: #0000FF;">),</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">row</span> <span style="color: #000080;font-style:italic;">-- scratch var</span>
<span style="color: #000000;">rqd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">5432</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">rqd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">row</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rqd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- row is now [0,5432]</span>
<span style="color: #000000;">row</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- rqd remains unchanged</span>
<span style="color: #000000;">rqd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">][</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">row</span> <span style="color: #000080;font-style:italic;">-- rqd[5][4][3][1] is now 1</span>
<span style="color: #000000;">rqd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- middle element of rqd[5][4] is now longer than the other two</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">rqd</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5</span><span style="color: #0000FF;">][</span><span style="color: #000000;">4</span><span style="color: #0000FF;">]</span>
<!--

View file

@ -0,0 +1,8 @@
-->
<span style="color: #0000FF;">?</span><span style="color: #008000;">"==1=="</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rqd</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"==2=="</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rqd</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"==3=="</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rqd</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span>
<!--

View file

@ -0,0 +1,18 @@
include ..\Utilitys.pmt
0 ( 5 4 3 2 ) dim
5432 ( 5 4 3 2 ) sset
( 5 4 3 2 ) sget print nl
( 5 4 3 ) sget var row
row print nl
row 1 2 set var row
row print nl
row ( 5 4 3 ) sset
0 10 repeat ( 5 4 2 ) sset
pstack

View file

@ -0,0 +1,41 @@
>>> from pprint import pprint as pp # Pretty printer
>>> from itertools import product
>>>
>>> def dict_as_mdarray(dimensions=(2, 3), init=0.0):
... return {indices: init for indices in product(*(range(i) for i in dimensions))}
...
>>>
>>> mdarray = dict_as_mdarray((2, 3, 4, 5))
>>> pp(mdarray)
{(0, 0, 0, 0): 0.0,
(0, 0, 0, 1): 0.0,
(0, 0, 0, 2): 0.0,
(0, 0, 0, 3): 0.0,
(0, 0, 0, 4): 0.0,
(0, 0, 1, 0): 0.0,
...
(1, 2, 3, 0): 0.0,
(1, 2, 3, 1): 0.0,
(1, 2, 3, 2): 0.0,
(1, 2, 3, 3): 0.0,
(1, 2, 3, 4): 0.0}
>>> mdarray[(0, 1, 2, 3)]
0.0
>>> mdarray[(0, 1, 2, 3)] = 6.78
>>> mdarray[(0, 1, 2, 3)]
6.78
>>> mdarray[(0, 1, 2, 3)] = 5.4321
>>> mdarray[(0, 1, 2, 3)]
5.4321
>>> pp(mdarray)
{(0, 0, 0, 0): 0.0,
(0, 0, 0, 1): 0.0,
(0, 0, 0, 2): 0.0,
...
(0, 1, 2, 2): 0.0,
(0, 1, 2, 3): 5.4321,
(0, 1, 2, 4): 0.0,
...
(1, 2, 3, 3): 0.0,
(1, 2, 3, 4): 0.0}
>>>

View file

@ -0,0 +1,6 @@
import numpy as np
a = np.array([[1, 2], [3, 4]], order="C")
b = np.array([[1, 2], [3, 4]], order="F")
np.reshape(a, (4,)) # [1, 2, 3, 4]
np.reshape(b, (4,)) # [1, 2, 3, 4]
np.reshape(b, (4,), order="A") # [1, 3, 2, 4]

View file

@ -0,0 +1,81 @@
>>> from numpy import *
>>>
>>> mdarray = zeros((2, 3, 4, 5), dtype=int8, order='F')
>>> mdarray
array([[[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]],
[[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]]], dtype=int8)
>>> mdarray[0, 1, 2, 3]
0
>>> mdarray[0, 1, 2, 3] = 123
>>> mdarray[0, 1, 2, 3]
123
>>> mdarray[0, 1, 2, 3] = 666
>>> mdarray[0, 1, 2, 3]
-102
>>> mdarray[0, 1, 2, 3] = 255
>>> mdarray[0, 1, 2, 3]
-1
>>> mdarray[0, 1, 2, 3] = -128
>>> mdarray[0, 1, 2, 3]
-128
>>> mdarray
array([[[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, -128, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]]],
[[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]],
[[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]]]], dtype=int8)
>>>

View file

@ -0,0 +1 @@
g = antenna.2.0

View file

@ -0,0 +1,4 @@
a = '"' /*set variable A to a quote character ["]. */
b = '~' /*set variable B to a tilde character [~]. */
h.a.b = '+++' /*set variable H.".~ to three pluses [+++]. */

View file

@ -0,0 +1,23 @@
/*REXX program shows how to assign and/or display values of a multi─dimensional array.*/
/*REXX arrays can start anywhere. */
y.=0 /*set all values of Y array to 0. */
/* [↑] bounds need not be specified. */
#=0 /*the count for the number of SAYs. */
y.4.3.2.0= 3**7 /*set penultimate element to 2187 */
do i=0 for 5
do j=0 for 4
do k=0 for 3
do m=0 for 2; #=#+1 /*bump the SAY counter.*/
/*the 1st SAY──► */ say 'y.'i"."j'.'k"."m '=' y.i.j.k.m
end /*m*/
end /*k*/
end /*j*/
end /*i*/
say
say '# of elements displayed = ' # /*should be 5 * 4 * 3 * 2 or 5! */
exit /*stick a fork in it, we're all done. */
/* [↓] other versions of the first (REXX) SAY instruction. */
say 'y.' || i || . || k || . || m '=' y.i.j.k.m
say 'y.'||i||.||k||.||m '=' y.i.j.k.m
say 'y.'i||.||k||.||m '=' y.i.j.k.m

View file

@ -0,0 +1,47 @@
# Raku supports multi dimension arrays natively. There are no arbitrary limits on the number of dimensions or maximum indices. Theoretically, you could have an infinite number of dimensions of infinite length, though in practice more than a few dozen dimensions gets unwieldy. An infinite maximum index is a fairly common idiom though. You can assign an infinite lazy list to an array and it will only produce the values when they are accessed.
my @integers = 1 .. Inf; # an infinite array containing all positive integers
say @integers[100000]; #100001 (arrays are zero indexed.)
# Multi dimension arrays may be predeclared which constrains the indices to the declared size:
my @dim5[3,3,3,3,3];
#Creates a preallocated 5 dimensional array where each branch has 3 storage slots and constrains the size to the declared size.
#It can then be accessed like so:
@dim5[0;1;2;1;0] = 'Raku';
say @dim5[0;1;2;1;0]; # prints 'Raku'
#@dim5[0;1;2;1;4] = 'error'; # runtime error: Index 4 for dimension 5 out of range (must be 0..2)
# Note that the dimensions do not _need_ to be predeclared / allocated. Raku will auto-vivify the necessary storage slots on first access.
my @a2;
@a2[0;1;2;1;0] = 'Raku';
@a2[0;1;2;1;4] = 'not an error';
# It is easy to access array "slices" in Raku.
my @b = map { [$_ X~ 1..5] }, <a b c d>;
.say for @b;
# [a1 a2 a3 a4 a5]
# [b1 b2 b3 b4 b5]
# [c1 c2 c3 c4 c5]
# [d1 d2 d3 d4 d5]
say @b[*;2]; # Get the all of the values in the third "column"
# (a3 b3 c3 d3)
# By default, Raku can store any object in an array, and it is not very compact. You can constrain the type of values that may be stored which can allow the optimizer to store them much more efficiently.
my @c = 1 .. 10; # Stores integers, but not very compactly since there are no constraints on what the values _may_ be
my uint16 @d = 1 .. 10 # Since there are and only can be unsigned 16 bit integers, the optimizer will use a much more compact memory layout.
# Indices must be a positive integer. Negative indices are not permitted, fractional indices will be truncated to an integer.

View file

@ -0,0 +1,58 @@
# Project : Multi-dimensional array
a4 = newlist4(5,4,3,2)
func main()
m = 1
for i = 1 to 5
for j = 1 to 4
for k = 1 to 3
for l = 1 to 2
a4[i][j][k][l] = m
m = m + 1
next
next
next
next
see "First element = " + a4[1][1][1][1] + nl
a4[1][1][1][1] = 121
see nl
for i = 1 to 5
for j = 1 to 4
for k = 1 to 3
for l = 1 to 2
see "" + a4[i][j][k][l] + " "
next
next
next
next
func newlist(x, y)
if isstring(x) x=0+x ok
if isstring(y) y=0+y ok
alist = list(x)
for t in alist
t = list(y)
next
return alist
func newlist3(x, y, z)
if isstring(x) x=0+x ok
if isstring(y) y=0+y ok
if isstring(z) z=0+z ok
alist = list(x)
for t in alist
t = newlist(y,z)
next
return alist
func newlist4(x, y, z, w)
if isstring(x) x=0+x ok
if isstring(y) y=0+y ok
if isstring(z) z=0+z ok
if isstring(w) w=0+w ok
alist = list(x)
for t in alist
t = newlist3(y,z,w)
next
return alist

View file

@ -0,0 +1,13 @@
object MultiDimensionalArray extends App {
// Create a regular 4 dimensional array and initialize successive elements to the values 1 to 120
val a4 = Array.fill[Int](5, 4, 3, 2) { m += 1; m }
var m = 0
println("First element = " + a4(0)(0)(0)(0)) // access and print value of first element
println("Last element = " + a4.last.last.last.last)
a4(0)(0)(0)(0) = 121 // change value of first element
println(a4.flatten.flatten.flatten.mkString(", "))
}

View file

@ -0,0 +1,7 @@
proc multilist {value args} {
set res $value
foreach dim [lreverse $args] {
set res [lrepeat $dim $res]
}
return $res
}

View file

@ -0,0 +1,6 @@
% multilist x 2
x x
% multilist x 2 3
{x x x} {x x x}
% multilist x 2 3 4
{{x x x x} {x x x x} {x x x x}} {{x x x x} {x x x x} {x x x x}}

View file

@ -0,0 +1,8 @@
% set ml [multilist 0 2 3 4]
{{0 0 0 0} {0 0 0 0} {0 0 0 0}} {{0 0 0 0} {0 0 0 0} {0 0 0 0}}
% lset ml 1 2 3 11
{{0 0 0 0} {0 0 0 0} {0 0 0 0}} {{0 0 0 0} {0 0 0 0} {0 0 0 11}}
% lset ml 1 1 4 12
{{0 0 0 0} {0 0 0 0} {0 0 0 0}} {{0 0 0 0} {0 0 0 0 12} {0 0 0 11}}
% lindex $ml 1 2 3
11

Some files were not shown because too many files have changed in this diff Show more