Data landscapes 2
Last updated
Last updated
To utilize complex expressions in python we need to load the mathematical functions library. This is expressed by adding the “import” statement prior to any other lines of code. It translates to “import all mathematical functions from the math module”.
The editor pops up after double clicking over the python component’s label. To make the text size larger you need to hold down the control key and use the scroll wheel of the mouse. To accept the code entered and close the python editor you need to click the “OK” button.
The python editor features also an additional “test” button which allows us to apply modifications without closing the window. This is very useful for experimentation as the graph is re-evaluated and the results are displayed on screen instantly.
Integer arithmetic functions are quite interesting as they produce repetitive patterns not unlike the sine and cosine, but they appear as “saw-tooth” forms. The modulo operation can be considered geometrically as a subdivision or repetition operation. Notice that this example uses the “interpolate” option of the surface, otherwise the “saw-tooth” form will not be as a sharp as in the figure below.
Another family of mathematical functions of interest are the “clipping set” including “min”, “max”, “ceil” and “floor”. These functions can help us introduce abrupt changes or discontinuities similar to modulo’s behaviour.
Practice
Instead of using min
and max
with a constant, try using another function for the second branch to appreciate the Boolean behaviour produced.
It is also perfectly valid to use conditional logic such as if
expressions. What is the effect of conditionals with vastly different expressions per branch?
We extensively used constant values in the expressions presented. This was often to perform simple operations such as translation and scaling. Instead of tweaking those and constantly updating the screen using the “test” button, it makes more sense to expose them as parameters connected to sliders. The topic of this section is creating a control structure around our expressions by exposing design parameters.
In graph above we incorporate translations by replacing x
and y
with x + ty
and y + ty
respectively. The parameters tx
and ty
represent displacements of the x
and y
inputs, or variables in the mathematical sense, supplied by the grid’s points.
For translation in the “z” direction we add a “tz” component which is equivalent to the idea of the “z + tz”. Therefore, the complete expression including translations becomes z = sin( ( x + tx ) * ( y + ty ) + tz
.
The ability to control translation allows us to traverse the domain of a function using sliders. For complex functions this offers an intuitive approach to detect interesting instances and to visualize the surface in an animated sense as seen in the figure.
Scaling follows the same concept but using multiplication instead of addition. We apply sx
, sy
and sz
factors by multiplying each of the original parameters. Again for the “z” direction we need to scale the entire expression. This is because if we assume our original function sin( x * y ) = z
as in f( x, y ) = z
, to achieve scaling we need sz * f( x, y )
. This expands to the complete form seen below which supports both translation and scaling z = sz * sin( ( x + tx ) * ( y + ty ) ) + tz
.
Scaling enables operations such as zooming within the domain of function. Here you need to also consider that our base grid has a finite resolution and that the surface drawn represents only an approximation of the true function’s shape. So modifying the scale sometimes interferes with the grid as the value from one point to the next changes too rapidly for the surface to accurately capture.
Practice
Try to use the translation and scaling parameters such the highly undulating region of this surface is captured with higher fidelity.
Translation and scaling are generic and useful but not the only possible parameters we may apply. Try to define a parameter that captures the number of repetitions using either trigonometric or modular expressions.
The ceil(value)
and floor(value)
functions produce the closest integer from a real number. For example ceil(3.4) = ceil(3.5) = ceil(3.6) = 4
, i.e. the decimal places are dropped and integer is rounded rightwards in the real line sense. In the same spirit floor(3.4) = floor(3.5) = floor(3.6) = 3
i.e. the number are rounded leftwards. Applying those functions to smooth surfaces, such an inverted paraboloid as seen below, produces the effect of terracing [].
The min(a, b)
and max(a, b)
functions return the smallest and largest of the parameters passed respectively. In fact they are so simple we can write them as min = a if( a < b ) else b
and in the same spirit we can write max = a if( a > b ) else b
The effect of the “min” and “max” functions resemble solid Boolean operations []. In the example below we clip a wave surface above and below a horizontal plane at level z = 1
using the min
and max
functions.
Python’s math library (or module) has many more functions [] to experiment with compared with what is available in the expressions component editor.