Application of tuple, lambda and map

## 1. Transmitting tuple

Place two GhPython components.
Connect them like this

(From side)

``````x=1
y=2
z=3
R=1```
```

(To side)
``````import rhinoscriptsyntax as rs
```

You can get

However, as the count of outputs increases, you need to add many output slots.
Hence, let's consider transmitting a single tuple that wraps all the values needed.

Now, we test transmitting a tuple from a component to component.
(From side)
````a=(((1,2,3),1),)`
```

Note that (a) is not treated as a tuple that contains "a" but just a single "a".
(a,) is treated as a tuple that contains "a".
Therefore, ((),) is treated as a tuple that contains a single tuple.
If you transmit a tuple it is automatically decomposed to items automatically by Grasshopper.
Hence, a tuple should be packed by a tuple so that it is transmitted safely.
(To side)
``````print x
((x,y,z),R)=x  #unpacking
print x
print y
print z
print R```
```

Then, let's modify the "to" side as
``````import rhinoscriptsyntax as rs
((x,y,z),R)=x  #unpacking
```

In turn, let's modify the "from" side as
``````a=list()
a.append(((1,2,3),1),)
a.append(((2,2,3),2),)
a.append(((3,2,3),3),)```
```

We get

Congraturation!

## 2. Switching functions

input: N (type hint=int)
output: a
Connect an integer slider to "N"
Set its minimum and maximum to 0 and 3.
Set the type of the slider to integer.

```import rhinoscriptsyntax as rs
funcList=list()
funcList.append(lambda x: (x,x**0.5,0))
funcList.append(lambda x: (x,x**1,0))
funcList.append(lambda x: (x,x**2,0))
funcList.append(lambda x: (x,x**3,0))
a=map(F2,map(funcList[N],range(4)))
```

## 3. rs.frange()

Since range() only supports integers, rhinoscriptsyntax provides rs.frange(), which supports floating point numbers.

```import rhinoscriptsyntax as rs
funcList=list()
funcList.append(lambda x: (x,x**0.5,0))
funcList.append(lambda x: (x,x**1,0))
funcList.append(lambda x: (x,x**2,0))
funcList.append(lambda x: (x,x**3,0))
a=map(F2,map(funcList[N],rs.frange(0,4,0.1)))
```

## 3. Merging functions

input: w1,w2,w3,w4 (type hint:float)
connect sliders to w1,w2,w3 and w4

```import rhinoscriptsyntax as rs
import math
w=[w1,w2,w3,w4] #list
funcList=list()
funcList.append(lambda x: (x**0.5))
funcList.append(lambda x: (math.cos(x)**1))
funcList.append(lambda x: (math.sin(x)**2))
funcList.append(lambda x: (x**3))
F1=lambda x: (x,w[0]*funcList[0](x)+
w[1]*funcList[1](x)+
w[2]*funcList[2](x)+
w[3]*funcList[3](x),0)

a=map(F2,map(F1,rs.frange(0,4,0.1)))
```

## 4. Gaussian function

Gaussian function represents a normal distribution.
Sometimes, it is used as just a "nice" distribution.
For further information, see
http://en.wikipedia.org/wiki/Gaussian_function
Let's try

```import rhinoscriptsyntax as rs
import math
A=1.0
B=1.0
C=1.0
F=lambda x:A*math.exp(-((x-B)**2)/(2*(C**2)))
a=list()
for i in rs.frange(-1.0,1.0,0.05):
```

You can get

Now, comment out A, B and C and take those values from input parameters.
Set up A, B and C as floating point number inputs.
Connect sliders to A, B and C.

```import rhinoscriptsyntax as rs
import math
#A=1.0
#B=1.0
#C=1.0
F=lambda x:A*math.exp(-((x-B)**2)/(2*(C**2)))
a=list()
for i in rs.frange(-1.0,1.0,0.05):
```

## 5. z=f(x,y) type surface

A lambda function can take multiple input parameters.
Let's draw an elementary parametric surface.
Generally, parametric surfaces are defined by
x=x(u,v)
y=y(u,v)
z=z(u,v)

Since this is too general, we only examine a special case of parametric surfaces that is defined by
z=f(x,y)
Note that this is a special case of the general case because this is equivalent to
x=u
y=v
z=f(u,v)

```import rhinoscriptsyntax as rs

F=lambda x,y:x**2+y**2
a=list()
for i in rs.frange(-1,1,0.1):
for j in rs.frange(-1,1,0.1):
```

You can get

In the end of the code, if you add
```a=rs.AddSrfPtGrid((21,21),a)
```

you can get a nurbs surface such as

Or, you can use a Grasshopper component instead

You need to set 21 to "U" in this case.
page revision: 32, last edited: 20 Oct 2013 01:03