Skip to main content
edited body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases

Explanation

Consider input [-3, -1, 5, 15, 29] as an example.

n:q    % Implicit input. Number of elements,. range,Range. subtractSubtract 1, element-wise
       % STACK: [0, 1, 2, 3, 4]
G      % Push input again
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29]
yz     % Duplicate from below. Number of non-zero elements
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29], 4
3$ZQ   % Fit polynomial with inputs x, y, degree
       % STACK: [3.7536e-16, -3.1637e-15, 2.0000, -8.8363e-15, -3]
Yo     % Round-, element-wise. Implicit display
       % STACK: [0, 0, 2, 0, -3]

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases

Explanation

Consider input [-3, -1, 5, 15, 29] as an example.

n:q    % Implicit input. Number of elements, range, subtract 1 element-wise
       % STACK: [0, 1, 2, 3, 4]
G      % Push input again
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29]
yz     % Duplicate from below. Number of non-zero elements
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29], 4
3$ZQ   % Fit polynomial with inputs x, y, degree
       % STACK: [3.7536e-16, -3.1637e-15, 2.0000, -8.8363e-15, -3]
Yo     % Round- element-wise. Implicit display
       % STACK: [0, 0, 2, 0, -3]

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases

Explanation

Consider input [-3, -1, 5, 15, 29] as an example.

n:q    % Implicit input. Number of elements. Range. Subtract 1, element-wise
       % STACK: [0, 1, 2, 3, 4]
G      % Push input again
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29]
yz     % Duplicate from below. Number of non-zero elements
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29], 4
3$ZQ   % Fit polynomial with inputs x, y, degree
       % STACK: [3.7536e-16, -3.1637e-15, 2.0000, -8.8363e-15, -3]
Yo     % Round, element-wise. Implicit display
       % STACK: [0, 0, 2, 0, -3]
added 594 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases

Explanation

Consider input [-3, -1, 5, 15, 29] as an example.

n:q    % Implicit input. Number of elements, range, subtract 1 element-wise
       % STACK: [0, 1, 2, 3, 4]
G      % Push input again
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29]
yz     % Duplicate from below. Number of non-zero elements
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29], 4
3$ZQ   % Fit polynomial with inputs x, y, degree
       % STACK: [3.7536e-16, -3.1637e-15, 2.0000, -8.8363e-15, -3]
Yo     % Round- element-wise. Implicit display
       % STACK: [0, 0, 2, 0, -3]

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases

Explanation

Consider input [-3, -1, 5, 15, 29] as an example.

n:q    % Implicit input. Number of elements, range, subtract 1 element-wise
       % STACK: [0, 1, 2, 3, 4]
G      % Push input again
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29]
yz     % Duplicate from below. Number of non-zero elements
       % STACK: [0, 1, 2, 3, 4], [-3, -1, 5, 15, 29], 4
3$ZQ   % Fit polynomial with inputs x, y, degree
       % STACK: [3.7536e-16, -3.1637e-15, 2.0000, -8.8363e-15, -3]
Yo     % Round- element-wise. Implicit display
       % STACK: [0, 0, 2, 0, -3]
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 12 bytes

n:qGyz3$ZQYo

The result is given with higher-order coefficients first, and may contain leading zeros.

Try it online! Or verify all test cases