What Is the Behavior of Symbolic nchoosek With n < 0 ?

62 views (last 30 days)
According to nchoosek:Algorithms
"If k < 0 or nk < 0, nchoosek(n,k) returns 0."
syms n k
f(n,k) = nchoosek(n,k)
f(n, k) = 
nn = sym(-7); kk = sym(2);
isAlways(nn - kk < 0)
ans = logical
1
f(nn,kk)
ans = 
28
The result should be 0 according to the algorithms section.
But given that it's not zero, where did it come from?
Not a simple factorial expression
try
factorial(nn)/factorial(kk)/factorial(nn-kk);
catch ME
ME.message
end
ans = 'Nonnegative integer or symbolic variable expected.'
Try an expanded version of f
f(n,k) = expand(f)
f(n, k) = 
The above expression does not match (to the eye) More About.
It also doesn't yield the original result
f(nn,kk)
ans = 
NaN
Nor does the simplified form (which also doesn't visually match More About)
f = simplify(f)
f(n, k) = 
f(nn,kk)
ans = 
NaN
Is the doc wrong and symbolic nchoosek(-7,2) should return 28? If so, where does 28 come from?
FWIW, nchoosek is not using abs(n)
f(abs(nn),kk)
ans = 
21
  3 Comments
Paul
Paul on 1 Sep 2025
Good idea!
binomial is actually a local function in sym/nchoosek.m. It leads to a call to nchoosek in the symbolic engine, which as best I can tell for the case at hand resolves to:
n = -7; k = 2;
((-1)^k*nchoosek(k - n - 1, k))
ans = 28
So I guess that explains the result so obtained, but I'm not sure how to interpret what that result actually means. 28 ways to choose 2 elements from a set of -7 elements?
If we make k odd, we get
nchoosek(sym(n),sym(3))
ans = 
which is again consistent with the formula above
k = 3;
((-1)^k*nchoosek(k - n - 1, k))
ans = -84
though again I have no idea how to interpret that result.
Torsten
Torsten on 1 Sep 2025
Edited: Torsten on 1 Sep 2025
n = -7;
k = 2;
n*(n-1)/(1*2)
ans = 28
n = -7;
k = 3;
n*(n-1)*(n-2)/(1*2*3)
ans = -84

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 1 Sep 2025
Edited: Stephen23 on 1 Sep 2025
It seems to use Newton's generalized binomial theorem:
which comes down to this:
nn = -7;
kk = 2;
prod(nn:-1:nn-kk+1) / factorial(kk)
ans = 28
The documentation is wrong.
  9 Comments
Paul
Paul on 3 Sep 2025 at 15:41
Edited: Paul on 3 Sep 2025 at 20:36
Getting a bit off topic from the original question, which was about sym inputs to sym/nchoosek, but ...
Both flavors of nchoosek support the (v,k) input. Suppose a user is writing code with
C = nchoosek(v,k)
with the intent that v is always a vector, though not known until execution time. The I suppose that code would have to look something like this
if numel(v) < k
C = zeros(0,k); % probabaly also need to ensure correct class of C
else
C = nchoosek(v,k);
end
to guard against a scalar input v that would then be interpreted as the (n,k) syntax.
dpb
dpb on 3 Sep 2025 at 15:44
Edited: dpb on 4 Sep 2025 at 16:07
"Note that there are (at least) two documentation pages for the nchoosek function. The document linked in this discussion is to the nchoosek method for sym objects, ..."
If one goes and compares, it's easy to see the sym objects page was derived starting with the base product doc; much of the verbiage is identical or only a very minor recast of it. Then, the symbolic stuff was tried to be addressed as edits(*).
I think the whole page for symbolic inputs other than positive integers needs a major revisiting given the expanded scope of inputs now allowed and the expanded results.
Describing the inputs for n and k as "Number of possible choices, ..." and "Number of selected choices, ..." is nonsensical for n, k <0 whether they're symbolic variables or not.
I think the Algorithms and/or a Tips section should be much more amplified; MATLAB documentation is generally pretty good in including references to the literature about where stuff comes from and in use/interpretation of results for the user who more than likely is not a mathematician but this particular page seems pretty weak.
I have noted before that much of the TB documentation isn't really up to the level of the main MATLAB core, however.
(*) At which point I'm always back to the Q? of how Mathworks actually writes/documents the official requirements for such functions -- is there an actual formal definition that does specify the result for all combinations of inputs or is the developer given the general description of what the function is supposed to do and it's then up to him/her/them to invent a solution and the resulting specification is then the implemented behavior after whatever internal review is done?

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!