开发者

routine to generate a 2d array from two 1d arrays and a function

开发者 https://www.devze.com 2022-12-25 23:21 出处:网络
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.

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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号