Do Symbolic simplify() and expand() of Expressions Involving nchoosek() Make Sense?

3 次查看(过去 30 天)
Define a simple nCk expression
syms k integer
e = nchoosek(2,k)
e = 
Evaluate at some values. Evaluation for k<0 and k>2 returns 0 per nchoosek:Algorithms (I wonder why that's the case, numeric nchoosek does not work like that)
subs(e,k,-1:5)
ans = 
Now simplify
es = simplify(e,5)
es = 
Error if trying to evaluate for 0 <= k <= 2, even though the original expression is perfectly sensible for this range of k (and would be the primary usage, I'd imagine)
try
subs(es,k,0:2)
catch ME
ME.message
end
ans = 'Division by zero.'
But evaluating for k where e should be 0 does work.
subs(es,k,[-1,3:5])
ans = 
Different expression
syms d integer
e = nchoosek(d+1,k)/factorial(d)/(d+1)
e = 
Expand it and we get something in terms of the gamma function
es = expand(e)
es = 
Evaluate the original expression at d = 2 for a range of k
subs(subs(e,d,2),k,-1:5)
ans = 
Evaluating the expanded expression returns a different result
subs(subs(es,d,2),k,-1:5)
ans = 
Simplify the expanded expression
es = simplify(es)
es = 
The simplified expression makes perfect sense for 0 <= k <= d + 1, but for other values throws an error
try
subs(subs(es,d,2),k,[-1,4:5])
catch ME
ME.message
end
ans = 'Nonnegative integer or symbolic variable expected.'
Of course, the simplified expression evaluates correctly for d = 2 and k = 0:3
subs(subs(es,d,2),k,0:3)
ans = 
I've always thought that simplify() and expand() should just be alternative forms of the original expression. But in these cases the numerical evaluation changes. Is this expected behavior?

回答(1 个)

Shlok
Shlok 2025-7-22
Hi,
When using MATLAB's "nchoosek", it returns 0 for values of k outside the range 0 to n, reflecting the combinatorial meaning i.e. you can't choose a negative number or more than the total elements.
However, when you apply "simplify" or "expand", the expression is rewritten purely algebraically and loses the piecewise definition that protects against undefined or invalid values of k. This leads to discrepancies in output and can get more problematic over multiple simplification or expansion steps.
So, for safe and expected behavior, it’s better to use the original "nchoosek" expression.
Hope it helps.
  1 个评论
Paul
Paul 2025-7-22
Sure. The point is that here we have a situation that using simplify or expand/simplify yields an expression that can evaluate to different values than the original, which can be problematic (and I don't think I've seen before, at least not that I can recall). Suppose nchoosek is included in a much larger expression. How would one go about operating on those expressions with simplify and/or expand? In some instances it may be possible to pull out the nchoosek, operate on what's left, and then stitch it all back together. But that might not be feasible, or at least not easy, in every case.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by