GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — implement dot for higher-dimensional arrays with Ix2 by EbrahimEldesoky · Pull Request #1602 · rust-ndarray/ndarray

Conversation

@EbrahimEldesoky

@EbrahimEldesoky EbrahimEldesoky commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

closes #1587

right now calling .dot() on anything above Ix2 doesn't work, so if you have a 3-D or 5-D array and want to multiply it by a matrix you have to manually reshape, dot, then reshape back.

this adds Dot<ArrayRef<A, Ix2>> for Ix3, Ix4, Ix5, Ix6, and IxDyn. the implementation flattens all axes except the last into a single "rows" dimension, delegates to the existing 2-D dot (which already handles BLAS), then reshapes the result back to the correct output shape.

for example:

use ndarray::prelude::*;
use ndarray::linalg::Dot;

let x = Array5::<f64>::zeros((3, 2, 5, 9, 12));
let y = Array2::<f64>::zeros((12, 13));
let z = x.dot(&y); // shape: (3, 2, 5, 9, 13)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@EbrahimEldesoky
implement dot for higher-dimensional arrays with Ix2
26fbaee
@EbrahimEldesoky

EbrahimEldesoky commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

hi @akern40 please when you have time check this pr

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nilgoyette

nilgoyette commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

One thing you could do before someone checks your PR is remove the formatting changes. It's hard[er] to know what you actually modified. We should probably format the whole project with a specific rule but it hasn't been done yet.

EbrahimEldesoky reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EbrahimEldesoky
EbrahimEldesoky force-pushed the nd-dot-ix2 branch from e252287 to 45415b4 Compare July 14, 2026 21:18
@EbrahimEldesoky
implement dot for higher-dimensional arrays with Ix2
642d18d
@EbrahimEldesoky
EbrahimEldesoky force-pushed the nd-dot-ix2 branch from 45415b4 to 642d18d Compare July 14, 2026 21:28
@EbrahimEldesoky
chore: fix pre-existing clippy::question_mark lints in indexes and it…
6f2d2d0
…erators
@EbrahimEldesoky

EbrahimEldesoky commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

One thing you could do before someone checks your PR is remove the formatting changes. It's hard[er] to know what you actually modified. We should probably format the whole project with a specific rule but it hasn't been done yet.

One thing you could do before someone checks your PR is remove the formatting changes. It's hard[er] to know what you actually modified. We should probably format the whole project with a specific rule but it hasn't been done yet.

Thank you @nilgoyette, Please take a look.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nilgoyette

nilgoyette commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

I don't know how common this operation is used (I never had to use it). If it is common, then yes we should add this feature to ndarray. Is it the same behavior as numpy dot?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EbrahimEldesoky

EbrahimEldesoky commented Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

I don't know how common this operation is used (I never had to use it). If it is common, then yes we should add this feature to ndarray. Is it the same behavior as numpy dot?

Yep, this lines up exactly with how numpy's dot works — for an N-D array against a 2-D matrix, it takes the sum-product over the last axis of the first array and the first axis of the second.
It's a pattern that shows up constantly in ML/data science too, like passing batched inputs shaped (Batch, Seq, Features) through a (Features, Outputs) weight matrix. Right now you'd have to flatten, multiply, then reshape everything back manually, which gets tedious fast and is an easy place to introduce bugs

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nilgoyette

nilgoyette commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

I'm not a fan of the reference_nd_dot function, but I understand that not using it would force you to hardcode long and unreadable arrays.

Good job. LGTM.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EbrahimEldesoky

EbrahimEldesoky commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

I'm not a fan of the reference_nd_dot function, but I understand that not using it would force you to hardcode long and unreadable arrays.

Good job. LGTM.

thank you

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bluss
bluss reviewed Jul 18, 2026
View reviewed changes

@bluss bluss left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relevant numpy documentation for dot is here:

https://numpy.org/doc/stable/reference/generated/numpy.dot.html


I'm basically not the maintainer here anymore - I think this is a good idea if we think enough about the interface and how to get the best out of performance. We should avoid whole-array-copying traps if possible.

The existing use of to_shape is pretty good for this, but will unfortunately cause copying in many cases.

Maybe this is a good enough compromise for now (with some improvements)?


In theory we could do it without any extra copying using matrixmultiply or blas like this:

Numpy had their example like this:

dot(a, b)[i,j,m] = sum(a[i,j,:] * b[:,m])

If axes i, j are contiguous together we can fold them together and compute as:

dot(a, b)[ij,m] = sum(a[ij,:] * b[:,m])

where ij is the combined axis. That's what's happening in this PR, more or less.

If axes i, j are not contiguous together (we can't reshape away that axis for free), then you could repair that by breaking up that axis in to multiple reductions, like this:

dot(a, b)[0,j,m] = sum(a[0,j,:] * b[:,m])  // sgemm i=0
dot(a, b)[1,j,m] = sum(a[1,j,:] * b[:,m])  // sgemm i=1
...         // and so on for the lenght of the axis

and then the operation still doesn't need extra copying of the ndarray. (of course both blas and matrixmultiply will copy the elements, efficiently, to their packing buffers anyway, but that's a necessary part of the matrix multiplication implementation, and usually quite optimized.)


Also for interest, general tensordot is already implemented for ndarray by the crate https://docs.rs/ndarray-einsum/ but I don't know its implementation. I think it actually might be a bit similar to the solution in this PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

EbrahimEldesoky added 2 commits July 18, 2026 17:49
@EbrahimEldesoky
refactor: optimize nd-dot by avoiding copy on standard layout, and it…
1145645
…erate axis for non-contiguous
@EbrahimEldesoky
bench: add dot_3d_f64_contiguous and dot_3d_f64_non_contiguous benchm…
2e13bb2
…arks
bluss
bluss reviewed Jul 18, 2026
View reviewed changes
Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

bluss
bluss reviewed Jul 18, 2026
View reviewed changes
Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

bluss
bluss reviewed Jul 18, 2026
View reviewed changes
Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

EbrahimEldesoky added 2 commits July 18, 2026 20:02
@EbrahimEldesoky
fix: address second round of feedback from bluss
ca2954d
@EbrahimEldesoky
fix: clean up borrow deref clippy error in nd_dot_non_contiguous
2ae90c2
@EbrahimEldesoky

EbrahimEldesoky commented Jul 18, 2026

Copy link
Copy Markdown
Contributor Author

@bluss, thanks for the guidance Please take a look at the updates Is there anything else?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bluss
bluss reviewed Jul 18, 2026
View reviewed changes
Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

bluss
bluss reviewed Jul 19, 2026
View reviewed changes
Comment thread src/linalg/impl_linalg.rs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bluss

bluss commented Jul 19, 2026

Copy link
Copy Markdown
Member

I'm mostly happy, good that we could push this in the right direction towards avoiding unnecessary copying.

Remaining things I would think of are, there is no new documentation, so users won't know that this operation is available. It should probably be documented, in brief.

And if possible I would try to reduce the code duplication, the existance of function nd_dot_non_contiguous is already good. I'm not fluent in ArrayRef yet, maybe it can be rewritten using ArrayRef? Maybe the new macro can generate less code and punt off to another common function?

EbrahimEldesoky reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

EbrahimEldesoky added 2 commits July 19, 2026 17:38
@EbrahimEldesoky
doc/refactor: reduce nd-dot code duplication using ArrayRef and add docs
c06b31e
@EbrahimEldesoky
style: format nd_dot_non_contiguous signature using nightly rustfmt
150a49d
@EbrahimEldesoky
EbrahimEldesoky requested a review from bluss July 19, 2026 15:00
@EbrahimEldesoky

EbrahimEldesoky commented Oct 12, 2026

Copy link
Copy Markdown
Contributor Author

@bluss when you have time please, follow up here

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@EbrahimEldesoky
Merge remote-tracking branch 'origin/master' into nd-dot-ix2
19db90e
# Conflicts:
#	src/indexes.rs
#	src/iterators/mod.rs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@bluss bluss Awaiting requested review from bluss

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[Feature request] ArrayRef<A, Ix2>.dot() for axis greater than Ix2

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@EbrahimEldesoky @nilgoyette @bluss
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.