Skip to content

Conversation

@forfudan
Copy link
Collaborator

Implements broadcast_to() for NDArray. Add tests.

It can broadcast an ndarray of any shape to any compatible shape. The data will be copied into the new array. An example goes as follows.

from numojo.prelude import *
from python import Python
fn main() raises:
    var np = Python.import_module("numpy")
    var a = nm.random.rand(Shape(2, 3))
    print(a)
    print(nm.routines.manipulation.broadcast_to(a, Shape(2, 2, 3)))
    print(np.broadcast_to(a.to_numpy(), (2, 2, 3)))
[[0.8073 0.5361 0.4442]
 [0.9378 0.1910 0.2421]]
2D-array  Shape(2,3)  Strides(3,1)  DType: f64  C-cont: True  F-cont: False  own data: True

[[[0.8073 0.5361 0.4442]
  [0.9378 0.1910 0.2421]]
 [[0.8073 0.5361 0.4442]
  [0.9378 0.1910 0.2421]]]
3D-array  Shape(2,2,3)  Strides(6,3,1)  DType: f64  C-cont: True  F-cont: False  own data: True

[[[0.8074 0.5361 0.4442]
  [0.9378 0.1911 0.2421]]
 [[0.8074 0.5361 0.4442]
  [0.9378 0.1911 0.2421]]]

@forfudan forfudan marked this pull request as ready for review January 30, 2025 21:28
@MadAlex1997 MadAlex1997 merged commit f05de8c into Mojo-Numerics-and-Algorithms-group:pre-0.6 Feb 2, 2025
2 checks passed
@forfudan forfudan deleted the bc branch February 2, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants