Boost logo

Ublas :

From: Manoj Rajagopalan (rmanoj_at_[hidden])
Date: 2006-12-02 13:20:12

Thanks Michael! So this is how I have understood the closure concept.
Correct me if I am wrong:

A closure is the generalization of a reference to include functions and
the arguments that a function call is bound to at the time the call is
made. This is possible only when functions are first-class objects,
i.e., they can be passed around just like variables can. For example, if
a function that computes sin(ax) is declared as double sin(double a,
double x), and we'd like to construct a reference to this sin function
for a particular value of 'a' and pass that to something like a graphing
function, then such a reference that includes the reference to an
argument as well as to the function would be called a closure. The
origin of this term is from functional programming where it is possible
to construct such (partial) bindings and pass them around to other

In uBLAS, expression templates are used to construct scalar-expressions,
vector-expressions and matrix-expressions so that they can be
efficiently evaluated. An expression encapsulating a binary operation
like addition must refer to the addition operation and to the two
operands being added. When addition operations occur at multiple places
in the code with different operands (which may themselves be
expressions), then each occurrence is encapsulated in a closure, i.e., a
reference to the same addition operation but different operand-pair. Two
such closures are identical if and only if they refer to the same
operation on the same set of operands.

Manoj Rajagopalan

Michael Stevens wrote:
> On Thursday, 30. November 2006 13:28, Gunter Winkler wrote:
>> On Wednesday 29 November 2006 23:04, Manoj Rajagopalan wrote:
>>> closure_type and const_closure_type appear often in ublas class
>>> declarations. What does closure mean and why is it required / useful?
>> This is a quite difficult topic. I never understood the concept in all
>> details, but I look at a closure as a mutable or constant reference to
>> something. The idea is to have
>> a) closure_type ... everything is mutable
>> b) const_closure_type ... everthing is const (read: add the const qualifier
>> wherever possible)
>> Is is required or useful: I don't use it, because it never worked the way I
>> thought.
> The name Closure comes from functional programming languages. I just looked at
> so I should know!!
> In principle the uBLAS closure types are not much more then references.
> They hold references for the variables that appear in the uBLAS expressions.
> So when you write
> prod (a, b)
> the expression is converted into a type that represents the binary operator
> (product) and referenced to a and b. When you assign this expression to
> something
> c = prod (a, b);
> the function implementing the prod operator access it's left and right hand
> sides using the closure types.
> I guess it makes sense to call these types closures as uBLAS expressions are
> functional in their nature. In fact the complete uBLAS expressions are really
> the closures. They combine code with a binding (as references) to variables
> in a function.
> In principle you can do stuff like:
> matrix_expression<Something complex> pab = prod (a, b);
> change a; change b;
> c = pab;
> In this case 'c' is computed using their values after they we changed as uBLAS
> expressions use references to a and b.
> Michael