从大型表达式中提取与模式匹配的表达式 [英] Extracting an expression matching a pattern from a large expression

查看:27
本文介绍了从大型表达式中提取与模式匹配的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含单个平方根的 Mathematica 表达式,示意性

I have a Mathematica expression that contains a single square root, schematically

expr = a / (b + Sqrt[c]);

其中 a,b,c 是大型表达式.我想提取 sqrt 下的表达式,例如通过匹配模式,例如

where a,b,c are large expressions. I would like to extract the expression under the sqrt, for instance by matching to a pattern, something like

Match[expr,Sqrt[x_]] // should return c

有没有简单的方法可以做到这一点?

Is there an easy way to do this?

推荐答案

理论上,这应该可以正常工作:

Theoretically, this should work correctly:

extractSqrt = Cases[ToBoxes@#, SqrtBox@x_ :> ToExpression@x, Infinity] &;

extractSqrt[expr]

这篇关于从大型表达式中提取与模式匹配的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆