josdejong/mathjs

aggregation operations on 2d arrays using axis 1 don't return an array type and return only MathScalarType

Open

#3,160 建立於 2024年2月19日

在 GitHub 查看
 (3 留言) (0 反應) (0 負責人)JavaScript (13,832 star) (1,298 fork)batch import
bughelp wantedtypescript

描述

Describe the bug On functions like mean, min, max, std, if you take some 2d number array and want to aggregate it along the 1 axis to retrieve a 1d array of output, you want to get some kind of array/matrix/vector type back. However, it only returns MathScalarType which causes a typescript error. I believe that this code in the types/index.d.ts file at line 2668 causes this:

  /**
   * @param A A single matrix
   * @param dimension The mean over the selected dimension
   * @returns The mean value
   */
  mean<T extends MathScalarType>(
    A: T[] | T[][],
    dimension?: number | BigNumber
  ): T

This has a return value of T, but it actually returns T[] when you have dimension=1 and we want the annotated type to match.

To Reproduce something like

const dataArray: number[][] = [[1, 2], [3, 4]];
const resultArray: number[] = math.mean(dataArray, 1);

should create an error saying "Type 'MathScalarType' is not assignable to type 'number[]'". But this works perfectly as intended and does actually return the answer correctly.

This happens on mean, min, max, std, and maybe more things that involve axis but I haven't tested them.

貢獻者指南

aggregation operations on 2d arrays using axis 1 don't return an array type and return only MathScalarType · josdejong/mathjs#3160 | Good First Issue