# 斐波那契式矩阵展开

25

``````[ 1 1 1 ]
[ 2 3 4 ]
``````

``````[ 1 1 1 2 ]
[ 2 3 4 7 ]
[ 3 4 5 9 ]
``````

### 例子：

``````Input:                     Output:

2, [ 0 0 ]                 [ 0 0 0 0 ]
[ 0 0 ]                 [ 0 0 0 0 ]
[ 0 0 0 0 ]
[ 0 0 0 0 ]

3, [ 1 1 1 ]               [ 1  1  1  2  3  5 ]
[ 2 3 4 ]               [ 2  3  4  7 11 18 ]
[ 3  4  5  9 14 23 ]
[ 5  7  9 16 25 41 ]
[ 8 11 14 25 39 64 ]
``````

8

# MATL，13 14 15 16 20 21字节

``````2*:"!tP2:Y)sv
``````

``````2*         % implicitly input number N and multiply by 2
:          % create vector [1,2,...,2*N]
"          % for loop: do this 2*N times
!        %   transpose. Implicitly input matrix in the first iteration
tP       %   duplicate and flip vertically
2:       %   vector [1,2]
Y)       %   pick submatrix formed by the first two rows
s        %   sum of each column
v        %   append as a new row
% end for
% implicit display
``````

1

Zgarb '16

@Zgarb当然！我怎么想念的？谢谢！！

MATL是否具有用于将数字加倍的内置功能？
Zgarb '16

@Zgarb号。您需要`2*`（后缀符号）。也许它应该内置一个字符，经常使用。也`2^`（正方形）。但是我用完了代码空间:-)

6

## J，19个字节

``````(v"1@v=.,[+&{:}:)^:
``````

``````  3 ((v"1@v=.,[+&{:}:)^:) 2 3 \$ 1 1 1 2 3 4
1  1  1  2  3  5
2  3  4  7 11 18
3  4  5  9 14 23
5  7  9 16 25 41
8 11 14 25 39 64
``````

## 说明

``````(v"1@v=.,[+&{:}:)^:  Left argument x, right argument y
(               )^:  Repeat x times:
v=.               Bind the following verb to v, and apply to y:
[    }:         y and y-without-last-item
+&{:           Sum of their last items
,                Append that to y
v"1@                  then apply v to columns
``````

3

# K，23个字节

``````{x(2({x,+/-2#x}'+)/)/y}
``````

``````  {x(2({x,+/-2#x}'+)/)/y}[3;(1 1 1;2 3 4)]
(1 1 1 2 3 5
2 3 4 7 11 18
3 4 5 9 14 23
5 7 9 16 25 41
8 11 14 25 39 64)
``````

ngn

3

# 果冻15 13 12字节

-1字节@ @Dennis

``````ṫ-S;@"Z
ÇḤ}¡
``````

``````ṫ-S;@"Z       Helper link. Input: x (2D array)
-              Numeric literal: -1
ṫ               Get x[-1:], i.e. last two rows in x
S             Sum
;@"          Append each to x. " is 'zipWith'; @ switches argument order.
Z         Transpose the array.
ÇḤ}¡          Main link. Input: a, n
Ç               Call the last link on a
Ḥ}             2n
¡            times.
``````

2

## ES6，134个字节

``````(n,a)=>[...a.map(b=>[...b,...Array(n)].map(c=>(c<1/0?0:c=a+d,d=a,a=c))),...Array(n)].map(b=>(b?0:b=[...a].map((c,j)=>c+d[j]),d=a,a=b))
``````

``````(n,a)=> // arguments n is number to expand, a is original array
[...
a.map(b=> // for each row in a
[...b,...Array(n)] // append n elements to the row
.map(c=>(c<1/0?0:c=a+d,d=a,a=c))) // scan the elements and fill the new ones by summing the previous two
,...Array(n)] // append n rows
.map(b=>(b?0:b=[...a].map((c,j)=>c+d[j]),d=a,a=b)) // scan the rows and fill the new rows by summing the previous two rows
``````

2

``````o%m=m++[o(+)(last m)\$last\$init m]
(!!).iterate(map(id%).(zipWith%))
``````

``````*Main> ( (!!).iterate(map(id%).(zipWith%)) ) [[1,1,1],[2,3,4]] 3
[[1,1,1,2,3,5],[2,3,4,7,11,18],[3,4,5,9,14,23],[5,7,9,16,25,41],[8,11,14,25,39,64]]
``````

``````(!!).iterate(    ...         )  -- repeatedly apply ... to the first agrument and
-- pick the iteration defined by the second arg
(zipWith%)   -- for one iteration add a new row and
map(id%)              -- then a new element at the end of each each row

o%m                             -- add row or element at the end of a row resp.
-- argument o is a "modify function"
--          m the whole matrix or a row
m++[    (last m)(last\$init m)] -- take m and append the result of combining the
-- last and 2nd last element of m
o(+)                       -- with a modified version of (+)
-- modification is none (aka. id) when adding an
-- element to the end of a row and
-- zipping elementwise (zipWith) when adding a row
``````

Digital Trauma

@DigitalTrauma：要么将这`o%m=...`一行（并且只有这一行）放在一个名为的文件中`fib-matrix.hs`。然后，您可以使用该`:l fib-matrix.hs`命令`ghci`来加载定义并调用主函数，如我的用法示例中所述。-或使用`let o%m=... in ( (!!). ... ) [[1,1,1]...] 3`
nimi 2016年

1
@DigitalTrauma：哦，有第三种方法：给主函数起个名字，例如`f=`在第二行前面加上一个：`f=(!!).iterate...`，将这两行保存在文件中并通过加载`l: <filename.hs>`。然后您可以致电`f [[1,1,1],[2,3,4]] 3`，等等
。– nimi

@DanielHill：有一个关于meta主题，它允许依赖全局助手函数的未命名函数。
nimi 2016年

2

## CJam，17 16字节

``````q~2*{~_2\$.+]z}*p
``````

### 说明

``````q~      e# Read and evaluate input.
2*      e# Double the iteration count.
{       e# Run this block that many times...
~     e#   Dump all rows on the stack.
_     e#   Copy the last row.
2\$    e#   Copy the penultimate row.
]     e#   Wrap all rows in a new array.
z     e#   Transpose such that the next iteration processes the other dimension.
}*
p       e#   Pretty-print.
``````

1

## 认真地，20个字节

``````,,τ"┬`;d@d@X+@q`M"£n
``````

``````,τ",┬`;d@d@X+@q`M"nkΣ£ƒ
``````

1

# Pyth，13 12字节

``````u+Rs>2dCGyEQ
``````

### 说明

``````u        yEQ     do 2*N times, starting with input matrix:
CG          transpose
+R                append to each row:
s                 sum of
>2d              last 2 elements of row
``````

1

# Matlab，60个字节

`A(end+1,:)=sum...`在弄清楚这种罕见的情况下，简单串联实际上在Matlab中更便宜之前，我首先是在弄乱Matlab的精美索引方法（即）。太糟糕了，我不得不将其转换为实际功能。应该与八度一起工作。

``````function A=f(A,n)
for i=1:2*n
A=[A;sum(A(end-1:end,:))]';end
``````

Digital Trauma

1

Sanchises

Digital Trauma

Sanchises

1

# Java，2179字节

``````import java.util.Scanner;

public class FebonnaciMatrix {
static Scanner scan=new Scanner(System.in);

public static void main(String[] args) {

int x,y;
System.out.println("For the Array to Work Upon:- ");

System.out.println("Enter the Row:- ");
int row=scan.nextInt();
System.out.println("Enter the Column:- ");
int col=scan.nextInt();

int inpArr[][]=new int[row][col];

System.out.println("Enter the values");
inpArr=inpValues(row,col);

System.out.println("The Input Array is:- ");
display(inpArr,row,col);

System.out.println("Input the Array size of Febonacci Array ");

System.out.println("Enter the Row");
int frow=scan.nextInt();
System.out.println("Enter the Column");
int fcol=scan.nextInt();

int febArr[][]=new int[frow][fcol];
febArr=copyValue(inpArr,febArr,row,col);

for(x=0;x<row;x++)
{
for(y=col;y<fcol;y++)
febArr[x][y]=febArr[x][y-2]+febArr[x][y-1];
}

for(x=row;x<frow;x++)
{
for(y=0;y<fcol;y++)
febArr[x][y]=febArr[x-2][y]+febArr[x-1][y];
}

System.out.println();
System.out.println("The Febonacci Array:-");
display(febArr,frow,fcol);
}

static void display(int[][] arr,int row,int col)
{
int x,y;
for(x=0;x<row;x++)
{
for(y=0;y<col;y++)
System.out.print(arr[x][y]+"\t");
System.out.println();
}
}

static int[][] inpValues(int row,int col)
{
int arr[][]=new int[row][col];
int x,y;
for(x=0;x<row;x++)
{
for(y=0;y<col;y++)
{
System.out.print("Enter the value:- ");
arr[x][y]=scan.nextInt();
}
}
return arr;
}

static int[][] copyValue(int[][] old, int[][] ne, int row,int col)
{
int x,y;
for(x=0;x<row;x++)
{
for(y=0;y<col;y++)
ne[x][y]=old[x][y];

}
return ne;
}

}``````

1

Digital Trauma

...查看代码高尔夫tag-wiki，尤其是我应该如何回答代码高尔夫球”？有什么提示吗？部分。还要注意，与许多其他语言相比，java众所周知很难精简为短代码。但是，这并不会让您感到困扰-如果您有一个很好的java答案，即使它比所有其他答案都长，它也很可能会很受欢迎。不要被所有弯弯曲曲的简短esolang答案所困扰-这个社区倾向于善于考虑语言障碍。
Digital Trauma '02

@ DigitalTrauma-谢谢...作为新手帮助我...我一定会通过链接并提出新的代码...
Dhruv Govila

Digital Trauma '02

Dhruv Govila '16

1

# Python，第103个105字节

``````f=lambda n,L:f(n-1,[l+[sum(l[-2:])]for l in L])if n else L
lambda n,L:zip(*f(n,map(list,zip(*f(n,L)))))
``````

1
`n>0`可以简单地是`n`，因为您从一个积极的开始，`n`当您达到`0`它的价值是假的。

1

# APL（Dyalog Classic），17个字节

``{⍉⍵,⊢/2+/⍵}⍣2⍣⎕⊢⎕``

0

# Perl 6的， 87个73  71字节

``````->\c,\m{for ^c {.[+*]=[+] .[*X-1,2]for m;m.push: [map {[+] m[*X-1,2;\$_]},m[0].keys]};m}
->\c,\m{for ^c {.[+*]=[+] .[*X-1,2]for m;m[+*]=[m[*-2;*] »+«m[*-1]]};m}
->\c,\m{for ^c {.[+*]=[+] .[*X-1,2]for m;m[+*]=[m[*-2;*]Z+m[*-1;*]]};m}``````
``````-> \c, \m {
for ^c { # 0 ..^ c

# each row
.[+*]                            # new column at the end of row (\$_)
= [+] .[ * X- 1,2 ]        # add up the last two entries in row (\$_)
for m; # for every row

# too bad this was longer than the above code
# m[*;+*]=map *+*,m[*;*-2,*-1]

# each column
m[ +* ]                 # add new row
= [             # make it an Array rather than a List
m[ *-2; * ] # the second to last row
m[ *-1 ]    # the last row
]
};

m # return the result
}``````

### 用法：

``````use v6.c;
# give it a lexical name
my &code = ->\c,\m{ … }

my @return = code 3,[[1,1,1],[2,3,4]];

put '[ ', \$_».fmt('%2d'), ' ]' for @return;

put '';

put @return.perl ~~ {S:g/' '//};``````
``````[  1  1  1  2  3  5 ]
[  2  3  4  7 11 18 ]
[  3  4  5  9 14 23 ]
[  5  7  9 16 25 41 ]
[  8 11 14 25 39 64 ]

[[1,1,1,2,3,5],[2,3,4,7,11,18],[3,4,5,9,14,23],[5,7,9,16,25,41],[8,11,14,25,39,64]]``````

Digital Trauma

@DigitalTrauma对不起，我应该写出用法，`my &code = ->\c,\m{ … }`以明确表明 `->\c,\m{ … }`需要用上面的代码替换。我通常使用隐式`\$_``@_`显式占位符参数，`\$^a`因为它们通常更短。我只是没有考虑过。还要确保您使用的是足够新的版本（`\$*PERL.compiler.version !before 2015.12`
@DigitalTrauma您也可以在freenode.net 上的＃perl6频道上使用camelia （这样）来运行代码（以`m: `