LIM : Language-Integrated Mathematical Computation

Theory, experiment, and scientific computation comprise three main tools for scientific research and technology development. With respect to computation, traditional scientific languages such as IDL (Interactive Data Language), APL, Fortran and MatLab were widely used, but they were also complex or errorprone when writing large programs because they lack some attributes of modern object-oriented languages. Here Language-Integrated Mathematic (LIM) computation was designed as a high-level, object-oriented extension for modern general-purpose programming languages. Matrix data structure is the base of LIM with a simpler and lightweight syntax like dynamic and special mathematic languages, such as matrix generator, slice, composition and deconstruction, while "function inference" is the core of LIM for automatic algorithm generation which is both dynamic and type safe. LIM provides streamlined mathematical and scientific computational facilities while allowing developers to use existing, familiar tools and techniques, enabling scientists and engineers to develop dataintensive and computationalintensive programs in a natural, user-friendly way handling big data era.


Introduction
Scientific computation is an integral part of modern scientific research and technology development.Much software is developed in programming languages employing mathematical computation to solve scientific problems [3][4][5].While traditional mathematical languages such as APL, Fortran and MatLab (Mathworks, Inc.) are used extensively in mathematical and scientific computation, they lack some important attributes of modern languages, such as generics (type parameter) and type safety (strong typing).For that reason it is often difficult to write, reuse and debug large-scale programs written in these languages [8].Modern languages such as C# (Microsoft corporation), Java (Oracle corporation) and C++ have been quite successful in business and general software applications, but extra work was required with minimal benefit and became cumbersome in applications requiring large scientific computations.
A considerable amount of research has been devoted to mathematical computation [2,7,8].Some new special languages such as IDL (Interactive Data Language for ENVI), open source R, Microsoft f# and Oracle Fortress have been created; and some libraries such as JScience for Java (http://www.jscience.org/),Mathdotnet for .NET (https://www.mathdotnet.com/)and NumPy (http://www.numpy.org/)for Python have been established.The software industry now has reached a stable point in the evolution of object-oriented (OO) programming technologies.It continues to be a big challenge in scientific and engineering programming to reduce the complexity of mathematical computation using OO technology.Rather than design an entirely new programming language or add libraries to meet the requirements of a particular area, a more general approach is taken here to create Language-Integrated Mathematic (LIM) by adding generalpurpose mathematical facilities to the OO Framework.
LIM adopts a formula translation syntax which is quite familiar to scientists and engineers.It bridges the worlds of OO and scientific computation, representing the logical prescription for bringing type-safe scientific computation to modern OO language, thus conferring "first-class citizenship" on scientific computation.

How LIM Works
To see how LIM works, we begin with a square root operation on a matrix.Below is the LIM code for C#.
If one were to compile and run this program, the output would appear as follows: 1.0, 2.0,4.0,5.0,7.0,8.0We can dissect the second statement in Figure 1 into the following three statements in Figure 2: The expression uses three operators -':'; '[]'; and 'Sqrt'.The first is a matrix generator; the second is an element/elements getter or setter by index/indexes (matrix slice); and the third is a mathematical function operator to find the square root of every el-  Matrix is the most important data structure in LIM.Generics technology is used to define arbitrary data types.This can maximize code reusability, type safety and performance and allow LIM to integrate with a wide variety of databases seamlessly and speedily.Most matrix operators such as arithmetical and Boolean calculations in MatLab and Fortran are implemented here on the matrix data type using OO with other important concepts such as matrix composite, matrix properties and matrix deconstruction, making it possible for developers to work LIM in a way with which they are already comfortable.The following C# code demos the concepts of matrix composite, matrix properties and matrix deconstruction mentioned above.(Figure 4)

"double[] Sqrt(double[] source)" was implicitly created by a mechanism called "function inference" (FI).
LIM can automatically generate program and implicitly process many loop and selection algorithms, greatly reducing the number of programming statements.

Function Inference
For a function call, if the user does not explicitly provide a function that exactly matches the input parameters, then the compiler will try to create an overloaded function according to the inference rules by properly calling visible functions which is called base function.
Function inference can generate most loop and selection algorithms dynamically for a matrix with the provided base functions.It is particularly suitable for mathematical, scientific and engineering computation, allowing scientists and engineers develop mathematical programs rapidly, directing more energy toward substantive, as opposed to computational, intricacies of scientific problems.
Two kinds of Function inferences will be discussed here:

Elemental Function Inference
Elemental functions are unary functions.The provided base function is a scalar quantity version which takes one atomic type (structure, class, etc.) as an input parameter.The return type can be any type or be void.Some examples of elemental operators are conversion operators and trigonometric functions, such as Sin, Cos, Tan et, al.
The automatically-generated elemental function inference works by calling the provided base function once for each member of the underlying array and returns the calculated values as an array with the same dimension as the input parameters.
For example, the prototype of the provided base function to return the sine value of a specified number is "double Sin (double d)" and the following statement calculates the sine of a matrix by function inference: (Figure 5) for the matrix parameter by FI.
Elemental function inference process generally behaves like this: (1) LIM will try to find a visible function with the same function name which accepts scale parameter of the same type as the matrix element.The found function is regarded as the base function.
(2) LIM will auto-generate the proper function prototype in addition to dynamically generating proper algorithm.In the above example, the generated function is double[] Sin(double[] A).
(3) The dynamically generated function will be called.Without LIM, many repeated loop and selection statements must be written by hand, making programs complex, error-prone and cumbersome.Other mathematical computation languages and dynamic languages can also handle matrix parameters directly, but they lost type information, thus no interpreter's or compiler's error will occur until running.

Aggregate Function Inference
An aggregate function receives a sequence of values as a whole (e.g., vector, array) for input parameters and returns the aggregated value.There are two kinds of aggregate function inferences: for single matrix parameter and for multiply matrix parameters.
The automatic-generated aggregate function inference works by signaling (calling) the provided operator once for each member of the underlying array and returns the result as a scalar quantity or array according to the input parameters.Important examples of aggregate operators include statistical operators such as Max, Min, and Product and so on.
For example, the following program uses Sum to accumulate the sum of total values over a numeric matrix, and then sum the matrix and itself Figure 6: The process of the above program can be shown as following Figure 7: (1) If the parameter is type of one dimension matrix, LIM will try to find a visible function with the same function name which accepts two scale parameters of the same type as the matrix element.The found function is regarded as base function.
For multiple parameter function inference, if the parameter is n (n>1) dimension matrix, LIM will try to search a visible function with the same function name which accepts n-1 dimension matrix parameter with the same element type.If no result got, compiler will try to search function taking in n-2 dimension matrix parameter, until a base function is found.
(2) LIM will auto-generate the proper function prototype in addition to dynamically generating proper algorithm.In the above example, two functions will be created, first is double Sum(double[] A) and the other double[] Sum(double[,] A).
(3) The dynamically generated function will be called.
In short, inferred functions are early binding and statically checked, so they are type-safe.FI enables compiler to optimize large-scale data processing such as parallel and optimal algorithm, so it provides rapid and secure development method for OO and functional programming (FP).
6 Conclusions LIM = matrix data structure + function inference, matrix is the base and FI is the core.Matrix is simple, lightweight data structure which makes LIM much like traditional mathematical and dynamic programming language, such as IDL, APL, Fortran and Python.LIM adopts an easy-to-read style particularly suited to rapid development of scientific and technological computation.Function inference is utilized for automatic program generation, enabling a compiler to produce faster and more efficient code.LIM enables higher levels of application performance, robustness and reliability by maintaining a single and easy-to-manage programming model.When available, parallel technologies can be employed to utilize multiple processors or cores for execution in the new age of concurrency and multi-core without any changes in the LIM programming model and source code.To date, harnessing the power of the GPU require programmers use of a GPUspecific programming model like CUDA, OpenCL, or OpenACC [6], and programming and optimizing on current many-core accelerated HPC systems is very challenging [1].Compiler for LIM can optimize for big data processing on GPU, or even on supercomputer automatically in the future.Beside, LIM is type safe extension for general purpose language which means that is can handle user interface, database, web, cloud computation very easily.Thus, performance loss and type mismatch considerably greatly in mixed programming does not exist in LIM.
Base on LIM architecture, many professional toolboxes can be developed easily and elegantly.LIM enables scientists and engineers to write computationally intensive programs in a more natural, user-friendly way than do traditional programming languages such as c, c++, Fortran and MatLab.In short, LIM adds a dimension of enjoyment and interest to the computational process.

Figure 1 Figure 2
Figure 1 Lim program written in C# to calculate the square root of a matrix

Figure 3
Figure 3 Data flow of program depicted in Figure1

Figure 4
Figure 4 Demos of matrix composite, matrix properties and matrix deconstruction

Figure 5
Figure 5 Demos of elemental function inference

Figure 6
Figure 6 Demos of aggregate function inference

Figure 7
Figure 7 Data flow and type flow by aggregate function inference