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

조회 수: 114 (최근 30일)
Paul
Paul 2025년 9월 1일 15:09
편집: dpb 2025년 9월 4일 16:07
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