views:

48

answers:

3

I'm guessing that there's a word for this concept, and that it's available in at least some popular languages, but my perfunctory search was fruitless.

A pseudocode example of what I'd like to do:

function foo(a, b) {
  return a * b  // EG
}

a = [ 1, 2, 3 ]
b = [ 4, 5, 6 ]
matrix = the_function_for_which_I_search(foo, [a, b] )
print matrix
=> [ [ 4, 8, 12], [5, 10, 15], [6, 12, 18] ]

// or
function concatenate(a,b)
  return a.b
}
print the_function_for_which_I_search( concatenate, [ a, b ])
=> [ [ '14', '24', '34'], ['15', '25', '35'], [16', '26', '36'] ]

In other words, function_for_which_I_search will apply the function given as its first argument to each combination of the elements of the two arrays passed as its second argument, and return the results as a two-dimensional array.

I would like to know if such a routine has a common name, and if it's available in a python module, cpan package, ruby gem, pear package, etc. I'm also wondering if this is a core function in other languages, maybe haskell or R?

A: 

Maybe merge or composition?

By the way if it exists, it is related to vectors (calculating the product of a matrix) not to arrays.. it's a more general operation that works with two matrices (also vectors are matrices) iff the width of the first is equal to the height of the second, but in this way you consider one of your arrays as trasposed to a column vector.

Jack
A: 

It sounds like you're looking for matrix multiplication. See this previous post to see if it fits:

http://stackoverflow.com/questions/428473/matrix-artihmetic-in-php

TMG
@TMG: It's similar. I'm basically looking for matrix foo. IE using an operation other than multiplication to generate the matrix contents. I checked out the PEAR package they referenced there but it does seem to be very specifically geared towards working with numerical matrices; I'd like to be able to operate on any data type.
intuited
A: 

As I understand it, you asked 'in language independent way, how do you do matrix creation and transforms given a list or one dimensional arrays as inputs' essentially.

In general, most languages implement arrays and n dimension arrays as easy-to-use substitutes for pointers. You create the matrix by stepping through the pointers represented by the arrays, create a new element that is the result of the desired transform (multiplication as your example) and create a new n dimensional matrix that is the result of that transform. Some languages (C, Pascal) you have to manage the memory allocated to the matrix. Others (Perl, Python, Awk, C++ somewhat) will create and manage the matrix memory on the fly.

If I use an array in Perl, for example:

$i=4;
$array[$i] = 100;               # human readable form for ${$ref_to_array}[offset]. 
$ref_to_array = \@array         # ref to the array
print "${$ref_to_array}[$i]\n"  # that array, element $i

In C, the same is:

#include <stdio.h>

int array[] = {1,2,3,4,100}; /* const array to avoid malloc for memory */
int *ptr;

int main(void)
{

    ptr = &array[0];     /* point to the first element of the array */
    int i=4;                /* the offset of 100 */

    printf("array[%d]=%d\n", i, array[i]); /* prints 100 */
    printf("ptr+%d=%d\n", i, *(ptr+i)) ;   /* prints 100 */

    return 0;
}

Each language would be different for how you build a matrix from those inputs, even though C and Perl are similar.

In Perl, here is a matrix multiply:

#!/usr/bin/perl
use strict;
use warnings;

sub foo  {
    my @rtr;
    my ($refa, $refb)=@_;
    for(my $i=0; $i<=$#{$refa}; $i++) {
        for(my $j=0; $j<=$#{$refb}; $j++) {
            $rtr[$i][$j]=$refa->[$i] * $refb->[$j];
            }
        }
    return \@rtr;   
    }

my @onea = (1, 2, 3);
my @oneb = (4, 5, 6);

my $rtr_ref=foo(\@onea,\@oneb);

for(my $i=0; $i<=$#{$rtr_ref}; $i++) {
    for(my $j=0; $j<=$#{@{$rtr_ref}[$i]}; $j++) {
        print "$rtr_ref->[$i][$j] ";
        }
    print "\n";
}

Output:

4 5 6 
8 10 12 
12 15 18 

In C, the algorithm is nearly the same, but all the pointer reference and dereferences are different.

In conclusion -- there are problems doing this in a "language independent way" The pointer reference and dereference is different. The memory management is different.

So choose your language then focus on the matrix. For Perl, look at PDL, for C look at GSL

drewk
@drewk: I'm looking for a way to streamline the nested for loops you've created, ie something like perl's `map()` function that would apply the passed block or expression to each combination of a tuple of input arrays. I'm looking for a language-independent *name* for this *concept*, so that I can google it up and find out if it exists and what it's called in various languages that I write code in. I could just use nested `map()` calls, but it seems like a readily encapsulable routine so I'm wondering if there is a better way.
intuited
@drewk: Thanks for the info on PDL; it sounds like the perl equivalent to numpy or scipy. I don't write much C code these days but if I start doing so again GSL might be helpful. I'm still not entirely sure if these libraries have a routine like I'm looking for; they may be too specialized. In any case I'm just sussing out if this is a somewhat commonly known function/concept.
intuited
I think what you are looking for is the ability to take two lists or vectors, apply an operation, and receive a matrix. This is common in Fortran with nested do loops and C with nested for loops. To get where you want, just add a reference to a subroutine that performs the op in the nested loop / map / whatever language idiom.Best...
drewk
I prefer to avoid using `for` loops for operations that aren't really iterative, ie in cases where all of the loop's iterations would be performed independently. In C or fortran this is unavoidable, but other languages have functions like `map()` available, so maybe something for this, too. Cheers.
intuited