LtU Forum

Languages best suited for scientific computing?

I work in a field where the standard for high-performance scientific computing is still Fortran (albeit Fortran 95 nowadays). The array-based nature of Fortran provides a relatively clean and intuitive syntax for solving the systems of equations often involved in numerical simulations. The simplicity of Fortran has also facilitated highly efficient Fortran compiler implementations.

However I'm searching for a more modern and general purpose scientific computing language. Fortran is probably not the ideal language for writing networked graphical applications, neither I would argue is MATLAB or IDL. SciPy has deservedly gained traction amongst scientists recently and it is certainly a very attractive option. Unfortunately "CPython" can exhibit underwhelming performance characteristics requiring inconvenient work-arounds for CPU intensive code (see here for an example).


I'm of the belief that the ideal language should also be functional and open-source. Some contenders I've tried:

  • SAC lacks many desirable features for a general purpose programming language.
  • Boo is one of the most promising new "main-stream" languages but multidimensional array operations are not a core feature (although it recently gained array slices). It is also tied to the CLR.
  • OCaml probably comes closest, although again multidimensional arrays/matrices are not first-class citizens and the syntax is unfamiliar for most scientists (although the "OCaml Whitespace Thing" might help here).

Any suggestions in my quest for a better language are welcome. And yes I'm aware of hacks in C++ (Blitz) and Java (JScience); neither of these are very promising going forward IMHO!

Variation of C's inline conditional

The C language has an inline conditional statement, in the form:
condition ? exprA : exprB
Now, many times exprA is a component of the condition, for example:
(somebigexpression) != 0 ? (somebigexpression) : (defaultexpression)

I'm looking for a form of the inline conditional that doesn't repeat the "somebigexpression" in the above situation, without assigning it to an intermediate variable. I was thinking something along the lines of this syntax:
(somebigexpression) !=? 0 : (defaultexpression)
where if the "?" follows a comparison operator, then if true it will return the left side of the comparison; else it returns the right side of the ":".

So two questions... 1) Would this be useful enough in practice to bother adding it to a language (i.e., does it make the code more clear v.s. assigning the original expression to a temp variable)? and 2) Does another language already have a syntax that accomplishes the same thing that I can borrow from?

Thanks.

Problematic data structure in functional language

I am trying to build an efficient data structure for representing boolean expressions in Erlang. I am having trouble producing a good data structure in a functional context, although there are very fast data structures for imperative languages like C. The data structure is to be used for a functional SAT solver.

A boolean expression (always in conjunctive normal form) can be described as a set of variables, such as {p,q,r...} where each variable in the set is mapped to one of three values true|false|indetermined yielding something like {{p->ind},{q->true},{r->ind},...}.

The expression is described as a set of sets of literals, where a literal is a variable with a possible negation (~). So we have something like {{~p,q},{~q,r}} which describes a formula that would usually be written (~p v q)&(~q v r). Each of the inner sets has the property that if one of its literals evaluates to true then the whole set can be removed from the outer set.

To being with we can represent the expression as a list of lists where the variable-name, negation and value are all stored in one tuple. [[{name,negation,value}]]

Now if we assign a value to a variable we then need to traverse every sublist ensuring a consistent assignment is made for every occurrence of the varia ble in the expression. However, as a reward for this effort we can easily check each sublist to see whether one of its members evaluates to true (meaning the sublist can be removed).

Alternately we could represent the expression as a list of lists where just the variable name and negation are recorded {name,negation} and have the assignment values stored as a separate mapping. This makes assignments fast and safer, but we still need to traverse every sublist to see that it does not now contain a literal evaluating to true. Is there a better way, I suspect that I am thinking about it in the wrong way. Thanks.

Writing practical memory management code with a strictly typed assembly language

Writing practical memory management code with a strictly typed assembly language, by Toshiyuki Maeda, and Akinori Yonezawa.

Memory management (e.g., malloc/free) cannot be implemented in traditional strictly typed programming languages because they do not allow programmers to reuse memory regions, in order to preserve memory safety. Therefore, they depend on external memory management facilities, such as garbage collection. Thus, many important programs that require explicit memory management (e.g., operating systems) have been written in weakly typed programming languages (e.g., C). To address the problem, we designed a new strictly and statically typed assembly language which is flexible and expressive enough to implement memory management. The key idea of our typed assembly language is that it supports variable-length arrays (the arrays whose size is not known until runtime) as language primitives, maintains integer constraints between variables, and keeps track of pointer aliases. Based on the idea, we implemented a prototype implementation of the language. We also implemented a small operating system kernel which provides a memory management facility with the language.

This paper forms part of Toshiyuki Maeda's thesis, where he also uses TALK (OCaml source code available) to build a certified x86 kernel called TOS (source code available). I'm surprised I hadn't heard of this work before, as it seems quite practical.

The paper does not discuss the limitations of TALK, though the thesis does. The most significant disadvantage is described in section 2.8.1:

The type system of TALK is flexible and expressive enough to implement practical memory management code (e.g., malloc/free). However, it is not enough to efficiently implement all the data structures that can be represented in ordinary typed languages, because the type system of TALK restricts pointer manipulation in some cases in order to keep track of aliasing relations between pointers. For example, generic graph data structures including directed acyclic graphs and cyclic graphs cannot be represented naturally in the type system of TALK. This is because once a memory region is encapsulated inside an existential package, the region cannot be accessed until the package is unpacked.
They suggest prior work designed to relax linearity restrictions can be applied to TALK to enable graph structures to be captured.

While TALK is a low-level assembly language, other higher level languages have attempted to capture safe low-level memory management as well.

New to FP

I am new to FP and have been struggling on an on & off basis for over a year now. I think now I have at least an elementary hold on various facets of the field. My interest lies primarily in theoretical & formal computer science. So could somebody please recommend me which language would be most suitable for my purposes??I mean it should help me grasp the underlying mathematics without getting me diverted into how it can be helpful in implementing "real" projects.

I know something of haskell and am now beginning ML. And have advanced knowledge of C#.Net including the functional extensions in version 3.0. Some pointers as to useful web tutorials or books will also be great.

thanking in advance
Barun

[Ask LTU] How to implement concurrent languages ?

Hi all,

I used EOPL and TAPL to learn to write interpreters for simple languages and found both books tremendously helpful.

I've been using a custom scheme-like language (generating C code )for some of my consulting work with great success.

Now I am trying to learn how to implement languages with concurrency constructs (threads, actors, message passing concurrency etc). I could try to hack the source code of an existing concurrent language, but I was wondering if there was a book to learn from ("Implementing Concurrent Languages"? :-) ), along the lines of EOPL, explaining the various issues and tradeoffs on *implementing* concurrent languages. But this maybe too much to expect, I am just looking for a starting point.

Amazon has a few books on concurrent programming, but I couldn't find any that focussed on *implementation* of concurrent languages.

Any pointers (books, papers, interpreters to hack etc) greatly appreciated. LTU was very helpful the last time I asked a question (on how to write an FFI), hence this question. (Editors, If this question is not appropriate, please delete it )

*Any* information will be gratefully accepted. Help!! :-)

Thanks in advance,

Ravi

Educational environments to learn programming

I will be teaching introductory programming to total newbies in Q3 of this year.
The programming language is totally up to me to decide.
I'm looking for a language+environment that will not place too high a demand on learning the editors/IDEs.

What would be your recommendations ?
I'm looking for something in line with:

Thanks.

Gavin

C - header files

Hello!!

I'm a fan of the C language for certain applications (mainly numerical computations). I'm fairly productive in it, but there is one thing I'm sick of: writing and organizing header files.

Are there any proposals to add a module system to the official C standard (like for C++)?

Sort of:

  import stdio, stdlib;

  module example_module;

  
  // exposed data structure
  struct some_public_structure { .. };

  static struct some_private_structure { .. }:

  // exposed function
  void public_function(..) {...}

  static void private_function(..) {...}

Excel as a different programming paradigm

(via Gamasutra via /.) Reminds me of the Subtexts of the world: a 3D graphics system in Excel, with programming rant. Pretty fun.

Type-safe solution to the expression problem in C#?

Posted this on Channel9 (http://channel9.msdn.com/ShowPost.aspx?PostID=386812), but didn't get much feedback, so I thought I'd repost here.

Basically, I've been looking at a type-safe approach for the expression problem in C#, taking advantage of the lambda expression runtime compilation command Expression.Compile() in C#'s Language Integrated Query (LINQ). The biggest drawback to the solution is that the compiled lambda functions take about 2x longer to execute on primitive expressions compared to non-generic solutions. I haven't yet found an EvalStream implementation that can avoid this overhead of lambda evaluation. I'm pretty new to functional programming, but I'd love to hear anyone's thoughts on the approach I'm trying here (see LinFu author Philip Laureano's comments (thanks Philip!): http://plaureano.blogspot.com/2008/02/linfudynamicobject-and-dynamic-object.html#c4431642200009960939)

using System;
using System.Linq.Expressions;

namespace TPLTest
{
public class TestGADT
{
public static void Main()
{
Number n1 = 5;
Number n2 = 4;
Console.WriteLine(n1 + n2);

Array array1 = new Array(1000).InitializeTo(10);
Array array2 = new Array(1000).InitializeTo(20);
Array array3 = array1 + array2;
Console.WriteLine(array3[0]);

Console.Read();
}
}

public struct Number where T : struct
{
private T numValue;
public static Number operator +(Number a, Number b) { return GADT.AddFunc(a.numValue, b.numValue); }
public static Number operator -(Number a, Number b) { return GADT.SubtractFunc(a.numValue, b.numValue); }
public static Number operator *(Number a, Number b) { return GADT.MultiplyFunc(a.numValue, b.numValue); }
public static Number operator /(Number a, Number b) { return GADT.DivideFunc(a.numValue, b.numValue); }

public static implicit operator Number(T myValue) { return new Number() { numValue = myValue }; }
public static implicit operator T(Number myNumber) { return myNumber.numValue; }
public override string ToString() { return numValue.ToString(); }
public T ToValue() { return numValue; }
}

public class Array where T : struct
{
internal T[] arrayInstance;

public Array() { }
public Array(int size) { arrayInstance = new T[size]; }
public Array(T[] existingArrayInstance) { arrayInstance = existingArrayInstance; }
public T this[int index] { get { return arrayInstance[index]; } set { arrayInstance[index] = value; } }
public int Length { get { return arrayInstance.Length; } }

public static implicit operator T[](Array myArray) { return myArray.arrayInstance; }
public static implicit operator Array(T[] myArrayInstance) { return new Array() { arrayInstance = myArrayInstance }; }

public static Array operator +(Array a, Array b) { return GADT.AddFunc.EvalStream(a, b); }
public static Array operator -(Array a, Array b) { return GADT.SubtractFunc.EvalStream(a, b); }
public static Array operator *(Array a, Array b) { return GADT.MultiplyFunc.EvalStream(a, b); }
public static Array operator /(Array a, Array b) { return GADT.DivideFunc.EvalStream(a, b); }

public T[] ToArray() { return arrayInstance; }
}

public static class ArrayExtensions
{
public static Array EvalStream(this Func myFunc, Array a, Array b) where T : struct
{
Array output = new Array { arrayInstance = new T[a.arrayInstance.Length] };
for (int i = 0; i output.arrayInstance[i] = myFunc(a.arrayInstance[i], b.arrayInstance[i]);
return output;
}
public static Array InitializeTo(this Array myArray, T value) where T : struct
{
for (int i = 0; i myArray[i] = value;
return myArray;
}
}

public static class GADT
{
public static Func AddFunc = typeof(GADTHelper).GetFunc("Add");
public static Func SubtractFunc = typeof(GADTHelper).GetFunc("Subtract");
public static Func MultiplyFunc = typeof(GADTHelper).GetFunc("Multiply");
public static Func DivideFunc = typeof(GADTHelper).GetFunc("Divide");

internal static class GADTHelper
{
public static double Add(double a, double b) { return a + b; }
public static float Add(float a, float b) { return a + b; }
public static int Add(int a, int b) { return a + b; }
public static long Add(long a, long b) { return a + b; }

public static double Subtract(double a, double b) { return a - b; }
public static float Subtract(float a, float b) { return a - b; }
public static int Subtract(int a, int b) { return a - b; }
public static long Subtract(long a, long b) { return a - b; }

public static double Multiply(double a, double b) { return a * b; }
public static float Multiply(float a, float b) { return a * b; }
public static int Multiply(int a, int b) { return a * b; }
public static long Multiply(long a, long b) { return a * b; }

public static double Divide(double a, double b) { return a / b; }
public static float Divide(float a, float b) { return a / b; }
public static int Divide(int a, int b) { return a / b; }
public static long Divide(long a, long b) { return a / b; }
}
}

public static class DynamicExtensions
{
public static Func GetFunc(this Type t, string method)
{
Expression body = Expression.Call(t.GetMethod(method));
Expression e = Expression.Lambda(body);
return e.Compile();
}
public static Func GetFunc(this Type t, string method)
{
Type[] types = new Type[] { typeof(T) };
var pArray = types.GetParameterExpressionArray();
Expression body = Expression.Call(t.GetMethod(method, types), pArray);
Expression e = Expression.Lambda(body, pArray);
return e.Compile();
}
public static Func GetFunc(this Type t, string method)
{
Type[] types = new Type[] { typeof(T1), typeof(T2) };
var pArray = types.GetParameterExpressionArray();
Expression body = Expression.Call(t.GetMethod(method, types), pArray);
Expression e = Expression.Lambda(body, pArray);
return e.Compile();
}
public static Func GetFunc(this Type t, string method)
{
Type[] types = new Type[] { typeof(T1), typeof(T2), typeof(T3) };
var pArray = types.GetParameterExpressionArray();
Expression body = Expression.Call(t.GetMethod(method, types), pArray);
Expression e = Expression.Lambda(body, pArray);
return e.Compile();
}
public static Func GetFunc(this Type t, string method)
{
Type[] types = new Type[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) };
var pArray = types.GetParameterExpressionArray();
Expression body = Expression.Call(t.GetMethod(method, types), pArray);
Expression e = Expression.Lambda(body, pArray);
return e.Compile();
}
private static ParameterExpression[] GetParameterExpressionArray(this Type[] types)
{
ParameterExpression[] pArray = new ParameterExpression[types.Length];
for (int i = 0; i pArray[i] = Expression.Parameter(types[i], "p" + i);
return pArray;
}
}
}

XML feed