Tuple (ANY Vector)

Vectors can be divided into tuples and typed vectors based on the data type of elements. Elements in typed vectors must be scalars with the same data type, whereas elements in tuples can be scalars or vectors with flexible data types. Therefore, the data type of tuples in DolphineDB is defined as ANY.

Tuples are always displayed with round brackets, for instance, “(1, 2, 3)”. When creating a typed vector with square brackets, if it contains vectors or elements of multiple data types, it will be converted to a tuple, for example, “[1, ‘A’]”, “[[1,2,3], [4,5]]”.

As we will see later in this chapter, however, it is possible to use square brackets to construct tuples.

Creating a tuple

(1) Comma-separated elements in round brackets. This always generates a tuple, no matter whether the elements are of the same type or not.

$ a=(1 2 3, `IBM`MSFT`GOOG, 2.5);
$ a;
([1,2,3],["IBM","MSFT","GOOG"],2.5)

$ a=(1, 2, 3);
$ typestr a;
ANY VECTOR

$ x=(1..10, `GOOG);
$ x;
([1,2,3,4,5,6,7,8,9,10],"GOOG")

$ x=1..10;
$ y=rand(100,10);
$ a=(x+y,x-y);
$ a;
([73,26,32,96,26,86,11,63,21,49],[-71,-22,-26,-88,-16,-74,3,-47,-3,-29])

We can use () to create an empty tuple.

$ x=();
$ x.append!(1..3);
([1,2,3])
$ x.append!(`GS);
([1,2,3],"GS")

$ x=(1,2,3);
$ x;
(1,2,3)
$ x.append!(`GS);
(1,2,3,"GS")

We cannot transform a typed vector to a tuple with the append! function

$ x=1 2 3;
$ x;
[1,2,3]
$ x.append!(`GS);
Incompatible type. Expected: INT, Actual: STRING

(2) space separated elements with different data types, or comma-separated elements of different data types in square brackets “[]”. Square brackets always attempt to create a typed vector first. If it fails, it will create a tuple instead.

$ a = 3 2012.02.03 `GOOG;
(3,2012.02.03,"GOOG")
$ typestr a;
ANY VECTOR

$ a=[[1,2,3,4,5,6,7,8,9,10],2,[`IBM, `MSFT,`GOOG]];
$ a;
([1,2,3,4,5,6,7,8,9,10],2,["IBM","MSFT","GOOG"])

$ x=1..10;
$ y=rand(100,10);
$ b=[x+y,x-y];
$ b;
([73,26,32,96,26,86,11,63,21,49],[-71,-22,-26,-88,-16,-74,3,-47,-3,-29])

(3) use function array with “any” as the first element and then specify individual elements:

$ a=array(any, 3); a[0]=1..10; a[1]=2; a[2]=`IBM`MSFT`GOOG;
$ a;
([1,2,3,4,5,6,7,8,9,10],2,["IBM","MSFT","GOOG"])

Accessing tuples

$ x=(1 2 3 4 5 6 7 8 9 10,(5 7 8, 11 3 5));
$ x;
([1,2,3,4,5,6,7,8,9,10],([5,7,8],[11,3,5]))

$ x[1];
([5,7,8],[11,3,5])

$ x[1,1];
[11,3,5]

$ x[1,1,1];
3

$ x[0 1];
([1,2,3,4,5,6,7,8,9,10],([5,7,8],[11,3,5]))

$ x[0 1,1];
(2,[11,3,5])

$ x[, 1];
(2,[11,3,5])

$ x[1, ,0 2];
([5,8],[11,5])

$ x[,1,1];
(2,3)

$ x[1 2];
(([5,7,8],[11,3,5]),)

Modifying tuples

We can append a tuple with new elements. We can also assign a new object to a particular element.

  1. Append an element to a tuple.

$ x=`C 120`BH 100;
$ x;
("C",120,"BH",100)

$ x.append!("AAPL");
("C",120,"BH",100,"AAPL")

$ x.append!(300);
("C",120,"BH",100,"AAPL",300)

$ x.append!(`IBM 600);
("C",120,"BH",100,"AAPL",300,("IBM",600))
  1. Modify an element of a tuple by assigning the new value to an object:

$ tp = [["aaa", "bbb"], "ccc"]
$ tp[0] = tp[0].replace(tp[0][1], "A")
$ tp
(["aaa","A"],["ccc"])

Computation on tuples

Tuples are designed to hold mixed data types, not for efficient computation. However, computations are still allowed on tuples if the elements are numbers.

We can apply functions such as max, min, prod, sum, cummin, cummax, cumprod, cumsum to tuples.

$ a=(2, [3, 5], 10);

$ max a;
[10,10]

$ prod a;
[60,100]

$ cumsum a;
(2,[5,7],[15,17])

We can apply logic or relationship operators to tuples

$ a=(2, [3, 5], 10);

$ a>3;
(0,[0,1],1)

$ a==2;
(1,[0,0],0)

$ isNull a;
[0,0,0]

We can apply math operators such as add, sub, mul, div, ratio, mod, pow and abs to tuples.

$ a=(2, [3, 5], 10);

$ a+2;
(4,[5,7],12)

$ a pow 3;
(8,[27,125],1000)

Tuple for multiple assignments

$ x,y=(1 2 3, 2:5);
$ x;
[1,2,3]
$ y;
2 : 5

Tuple for a function returning more than one value

$ def foo(a,b){return a+b, a-b};

$ x = foo(15,10);

$ x;
(25,5)

$ typestr x;
ANY VECTOR