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

113 次查看(过去 30 天)
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 个评论
Paul
Paul 2025-9-1,17:14
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 2025-9-1,17:34
编辑:Torsten 2025-9-1,17:36
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

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2025-9-1,17:43
编辑:Stephen23 2025-9-1,19:22
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 个评论
Paul
Paul 2025-9-3,15:41
编辑:Paul 2025-9-3,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 2025-9-3,15:44
编辑:dpb 2025-9-4,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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by