Message Boards Message Boards

0
|
4119 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Input output in Wolfram Cloud

This is a starter query. I know if I enter 2+2 and press enter, I get 4.

Similarly, I enter expr = 5x + x^3 + Sin[x] and press enter. Next, I want to perform an operation with it, which is to extract the power of x , i.e., 3. This is not clear to me, how to single out this operation on Wolfram Cloud to do so. All I see is a new line after expr = 5x + x^3 + Sin[x] and enter.

POSTED BY: Rajeev Bagra
2 Replies
Posted 2 years ago

Crossposted here.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Try

expr=5x+x^3+Sin[x];
Replace[expr,__+x^a_->a]

and that should display 3

What that is doing is replacing everything in expr that matches the pattern _+x^a (and that is your entire expression) with whatever the a matched, which is just your exponent, and that will give you the desired 3.

You could in this specific example do

expr[[2,2]]

and that would extract the second item in expr, which will be x^3, and then extract the second item inside that, which will be your desired 3.

POSTED BY: Bill Nelson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract