|
25 | 25 | "create_diagonal", |
26 | 26 | "expand_dims", |
27 | 27 | "isclose", |
| 28 | + "kron", |
28 | 29 | "nan_to_num", |
29 | 30 | "one_hot", |
30 | 31 | "pad", |
@@ -479,6 +480,101 @@ def isclose( |
479 | 480 | return _funcs.isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan, xp=xp) |
480 | 481 |
|
481 | 482 |
|
| 483 | +def kron( |
| 484 | + a: Array | complex, |
| 485 | + b: Array | complex, |
| 486 | + /, |
| 487 | + *, |
| 488 | + xp: ModuleType | None = None, |
| 489 | +) -> Array: |
| 490 | + """ |
| 491 | + Kronecker product of two arrays. |
| 492 | +
|
| 493 | + Computes the Kronecker product, a composite array made of blocks of the |
| 494 | + second array scaled by the first. |
| 495 | +
|
| 496 | + Equivalent to ``numpy.kron`` for NumPy arrays. |
| 497 | +
|
| 498 | + Parameters |
| 499 | + ---------- |
| 500 | + a, b : Array | int | float | complex |
| 501 | + Input arrays or scalars. At least one must be an array. |
| 502 | + xp : array_namespace, optional |
| 503 | + The standard-compatible namespace for `a` and `b`. Default: infer. |
| 504 | +
|
| 505 | + Returns |
| 506 | + ------- |
| 507 | + array |
| 508 | + The Kronecker product of `a` and `b`. |
| 509 | +
|
| 510 | + Notes |
| 511 | + ----- |
| 512 | + The function assumes that the number of dimensions of `a` and `b` |
| 513 | + are the same, if necessary prepending the smallest with ones. |
| 514 | + If ``a.shape = (r0,r1,..,rN)`` and ``b.shape = (s0,s1,...,sN)``, |
| 515 | + the Kronecker product has shape ``(r0*s0, r1*s1, ..., rN*SN)``. |
| 516 | + The elements are products of elements from `a` and `b`, organized |
| 517 | + explicitly by:: |
| 518 | +
|
| 519 | + kron(a,b)[k0,k1,...,kN] = a[i0,i1,...,iN] * b[j0,j1,...,jN] |
| 520 | +
|
| 521 | + where:: |
| 522 | +
|
| 523 | + kt = it * st + jt, t = 0,...,N |
| 524 | +
|
| 525 | + In the common 2-D case (N=1), the block structure can be visualized:: |
| 526 | +
|
| 527 | + [[ a[0,0]*b, a[0,1]*b, ... , a[0,-1]*b ], |
| 528 | + [ ... ... ], |
| 529 | + [ a[-1,0]*b, a[-1,1]*b, ... , a[-1,-1]*b ]] |
| 530 | +
|
| 531 | + Examples |
| 532 | + -------- |
| 533 | + >>> import array_api_strict as xp |
| 534 | + >>> import array_api_extra as xpx |
| 535 | + >>> xpx.kron(xp.asarray([1, 10, 100]), xp.asarray([5, 6, 7]), xp=xp) |
| 536 | + Array([ 5, 6, 7, 50, 60, 70, 500, |
| 537 | + 600, 700], dtype=array_api_strict.int64) |
| 538 | +
|
| 539 | + >>> xpx.kron(xp.asarray([5, 6, 7]), xp.asarray([1, 10, 100]), xp=xp) |
| 540 | + Array([ 5, 50, 500, 6, 60, 600, 7, |
| 541 | + 70, 700], dtype=array_api_strict.int64) |
| 542 | +
|
| 543 | + >>> xpx.kron(xp.eye(2), xp.ones((2, 2)), xp=xp) |
| 544 | + Array([[1., 1., 0., 0.], |
| 545 | + [1., 1., 0., 0.], |
| 546 | + [0., 0., 1., 1.], |
| 547 | + [0., 0., 1., 1.]], dtype=array_api_strict.float64) |
| 548 | +
|
| 549 | + >>> a = xp.reshape(xp.arange(100), (2, 5, 2, 5)) |
| 550 | + >>> b = xp.reshape(xp.arange(24), (2, 3, 4)) |
| 551 | + >>> c = xpx.kron(a, b, xp=xp) |
| 552 | + >>> c.shape |
| 553 | + (2, 10, 6, 20) |
| 554 | + >>> I = (1, 3, 0, 2) |
| 555 | + >>> J = (0, 2, 1) |
| 556 | + >>> J1 = (0,) + J # extend to ndim=4 |
| 557 | + >>> S1 = (1,) + b.shape |
| 558 | + >>> K = tuple(xp.asarray(I) * xp.asarray(S1) + xp.asarray(J1)) |
| 559 | + >>> c[K] == a[I]*b[J] |
| 560 | + Array(True, dtype=array_api_strict.bool) |
| 561 | + """ |
| 562 | + if xp is None: |
| 563 | + xp = array_namespace(a, b) |
| 564 | + |
| 565 | + a, b = asarrays(a, b, xp=xp) |
| 566 | + |
| 567 | + if ( |
| 568 | + is_cupy_namespace(xp) |
| 569 | + or is_jax_namespace(xp) |
| 570 | + or is_numpy_namespace(xp) |
| 571 | + or is_torch_namespace(xp) |
| 572 | + ): |
| 573 | + return xp.kron(a, b) |
| 574 | + |
| 575 | + return _funcs.kron(a, b, xp=xp) |
| 576 | + |
| 577 | + |
482 | 578 | def nan_to_num( |
483 | 579 | x: Array | float | complex, |
484 | 580 | /, |
|
0 commit comments