从两个一维数组和一个函数生成二维数组的例程

发布于 2024-08-27 14:14:23 字数 665 浏览 8 评论 0原文

我猜测这个概念有一个词,并且至少在一些流行语言中可用,但我的敷衍搜索毫无结果。

我想要做的伪代码示例:

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'] ]

换句话说, function_for_which_I_search 会将作为其第一个参数给出的函数应用于作为其第二个参数传递的两个数组的元素的每个组合,并将结果作为双 -维数组。

我想知道这样的例程是否有一个通用名称,以及它是否在 python 模块、cpan 包、ruby gem、pear 包等中可用。我也想知道这是否是其他语言中的核心函数,也许哈斯克尔还是R?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2024-09-03 14:14:23

也许合并组合

顺便说一句,如果它存在,它与向量(计算矩阵的乘积)相关,而不是与数组相关。这是一个更通用的操作,适用于两个矩阵(向量也是矩阵),前提是第一个矩阵的宽度等于第二个的高度,但通过这种方式,您可以将其中一个数组视为转置为列向量。

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.

淡忘如思 2024-09-03 14:14:23

据我了解,您本质上问“以独立于语言的方式,如何创建矩阵并转换给定的列表或一维数组作为输入”。

一般来说,大多数语言都将数组和 n 维数组实现为易于使用的指针替代品。您可以通过逐步执行数组表示的指针来创建矩阵,创建一个作为所需变换结果的新元素(例如乘法),并创建一个作为该变换结果的新 n 维矩阵。某些语言(C、Pascal)您必须管理分配给矩阵的内存。其他(Perl、Python、Awk、C++)将动态创建和管理矩阵内存。

例如,如果我在 Perl 中使用数组:

$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

在 C 中,同样的情况是:

#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;
}

尽管 C 和 Perl 相似,但每种语言在如何从这些输入构建矩阵方面都会有所不同。

在 Perl 中,这是一个矩阵乘法:

#!/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";
}

输出:

4 5 6 
8 10 12 
12 15 18 

在 C 中,算法几乎相同,但所有指针引用和取消引用都不同。

总之——以“独立于语言的方式”执行此操作存在问题指针引用和取消引用是不同的。内存管理不同。

因此,选择您的语言,然后关注矩阵。对于 Perl,请查看 PDL,对于 C,请查看 GSL

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文