Skip to content

Broadcast Origin as a scalar by default?Β #181

@GregPlowman

Description

@GregPlowman

I often want to create several OffsetVectors with the same index offset.
For example, a vector of OffsetVectors.

This is a bit verbose:

OA = [ OffsetVector([1,2,3], 0:2), OffsetVector([4,5,6], 0:2) ]

This is better:

A = [ [1,2,3], [4,5,6] ]
OA = OffsetVector.(A, Ref(0:2))

Ref is required here to prevent indices 0:2 from being broadcast.

But I would like to specify just the starting index.
The following works but is also a bit too long.

A = [ [1,2,3], [4,5,6] ]
OA = OffsetVector.(A, Ref(OffsetArrays.Origin(0)))

Ideally I could omit Ref and OffsetArrays qualifiers:

A = [ [1,2,3], [4,5,6] ]
OA = OffsetVector.(A, Origin(0))

I can get what I want with this.

using OffsetArrays: Origin
Base.Broadcast.broadcastable(o::OffsetArrays.Origin) = Ref(o)

Are there problems with this approach?
Any better ways to reduce verbosity?
Could Origin be broadcast as a scalar by default?

EDIT:
I just realised using the offset directly will work:

A = [ [1,2,3], [4,5,6] ]
OA = OffsetVector.(A, -1)

Doesn't really capture the intent as clearly as OffsetVector.(A, Origin(0)) but it is most concise!

Perhaps the OffsetArray constructor should have taken starting index rather than offset? Then Origin wouldn't be needed? But I suppose then Offset would be wanted?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions