Overloading Lua

1) Math Library

Lua’s math library comes with essential mathematical functions; however, these only work on a single number. As we know, ScienceSuit is equipped with different types of data structures and the overloaded math functions, which are under std library, work on these data structures and also on Lua tables.

Overloaded functions: abs, acos, asin, atan, ceil, cos, cosh, exp, floor, ln, log, log10, sin, sinh, sqrt, tan, tanh.

Let’s do a demo with cos function. The same rationale is applicable to the other above-mentioned functions as well.

Using Lua's math library:

>>math.cos(5)
0.283662

Same result can be obtained using std library.

>>std.cos(5)
0.283662

Lua table (return type is a table):

>>t={1,2,3,4}
>>std.cos(t)
0.540302    -0.416147    -0.989992    -0.653644

 

Vector (return type is a vector)

>>v=std.tovector{1, 2, 3, 4}
>>std.cos(v)
0.540302    -0.416147    -0.989992    -0.653644    COL

 

Matrix (return type is a matrix)

>>m=std.tomatrix{ {1,2} , {3,4} }
>>std.cos(m)
0.540302       -0.416147
-0.989992      -0.653644

 

2) type() and next()

The functions, namely next and type, provided by Lua were overloaded for a better harmony with the framework.

2.1) type()

Inherently, Lua has 8 data types:

  1. nil
  2. boolean
  3. number
  4. string
  5. function
  6. userdata
  7. thread
  8. table

In ScienceSuit the types are reported by the function type is as expected:

>>a
nil

>>a=3
>>type(a)
number

>>s="abc"
>>type(s)
string

However, there is a catch with userdata and table. For example: Vector and Matrix are actually userdata types and say, if variables v and m were Vector and Matrix, respectively, a call to type(v) or type(m), would have only returned as userdata. Therefore, there would be no way of knowing the difference between v and m, except that they are userdata. Thus, for accuracy and convenience, type() has been overloaded.

For data type table, the scenario is slightly different: A call to a regular table would return a value as table, which is expected.

>>t={1,2,3}
>>type(t)
table

However, had we provided a metamethod __name(), a call to function type() would return the value returned by the function __name() which must be of type string.

function Food:__name()
    return "Food"
end

Now that a metamethod, namely __name(), is provided to the Food data structure, all variables created using the Food data structure will be reported as “Food” by function type().

>>milk=Food.new{water=88.13, protein=3.15, CHO=4.80, lipid=3.25, ash=0.67}
>>type(milk)
Food

Note that, the data structure name and the value reported by function __name() does not have to be the same. However, for convenience it is recommended that they are the same.

2.2) next()

In Lua, the function next() only works with tables. Therefore, to be able to use it with the provided data structures it has been overloaded.

Please note that, for a data structure to be able to benefit from this, it must implement a metamethod named as next(). Otherwise, an error will be thrown.

>>v=std.tovector{10,20,30,40}
>>key,value=next(v,nil)
>>key
1
>>value
10