# Vectors

Vectors in OpenSCAD are a collection (or list or table) of numeric values, boolean values, variables, vectors, strings or any combination thereof. They can also be expressions which evaluate to one of these. A vector has square brackets, [] enclosing zero or more items (elements or members), separated by commas. A vector can contain vectors, which contain vectors, etc. The information here also applies to lists and tables which use vectors for their data.

### examples

```   [1,2,3]
[a,5,b]
[]
[5.643]
["a","b","string"]
[[1,r],[x,y,z,4,5]]
[3, 5, [6,7], [[8,9],[10,[11,12],13], c, "string"]
[4/3, 6*1.5, cos(60)]
```
``` use in OpenSCAD:
cube( [width,depth,height] )           // optional spaces shown for clarity
translate( [x,y,z] )
polygon( [ [x0,y0],  [x1,y1],  [x2,y2] ] )
```

### creation

Vectors are created by writing the list of elements, separated by commas, and enclosed in square brackets. Variables are replaced by their values.

```  cube([10,15,20]);
a1 = [1,2,3];
a2 = [4,5];
a3 = [6,7,8,9];
b  = [a1,a2,a3];    // [ [1,2,3], [4,5], [5,7,8,9] ]  note increased nesting depth
```

### elements within vectors

Elements within vectors are numbered from 0 to n-1 where n is the length returned by len(). Address elements within vectors with the following notation:

```e           // element no 5 (sixth) at   1st nesting level
e        // element 2 of element 5    2nd nesting level
e     // element 0 of 2 of 5       3rd nesting level
e  // element 1 of 0 of 2 of 5  4th nesting level
```
example elements with lengths
```e = [ , [], [3,4,5], "string", "x", [[10,11],[12,13,14],[[15,16],]] ];  // length 6

e          1       
e          0       []
e          3       [ [10,11], [12,13,14], [[15,16],] ]
e       3       [ 12, 13, 14 ]
e       2       [ [15,16],  ]
e    2       [ 15, 16 ]
e undef   16

e          6       "string"
e[3 ]      1       "r"

s = [2,0,5]; a = 2;
e[s[a]]       3       [ [10,11], [12,13,14], [[15,16],] ]
```

# Operators

## concat

[Note: Requires version 2015.03 or later]

Concat() combines the elements of 2 or more vectors into a single vector. No change in nesting level is made.

``` vector1 = [1,2,3]; vector2 = ; vector3 = [5,6];
new_vector = concat(vector1, vector2, vector3) // [1,2,3,4,5,6]

string_vector = concat("abc","def");                 // ["abc", "def"]
one_string = str(string_vector,string_vector); // "abcdef"
```

## len

len() is a function which return the length of vectors or strings.

vector
Returns the number of elements at this level.
Indices of elements are from  to [length-1].
Single values, which are not vectors, return undef.
string
Returns the number of characters in string.

# Vector Math Operators

The vector-number operators take a vector and a number as operands and produce a new vector.

 * multiply all vector elements by number / divide all vector elements by number

### Vector Operators

The vector operators take vectors as operands and produce a new vector.

 + add element-wise - subtract element-wise

The "-" can also be used as prefix operator to element-wise negate a vector.

### Vector Dot-Product Operator

The vector dot-product operator takes two vectors as operands and produces a scalar.

 * sum of vector element products

### Matrix Multiplication

Multiplying a matrix by a vector, vector by matrix and matrix by matrix

 * matrix/vector multiplication