2

I have a string as

pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)

i need to get it as

pow(sin(4*x+5),2)+pow(cos(4*x+3),3)/pow(tan(x),5)

What i tried was

1)split the expression based on operators(+,-,/,*) between pow into single units.

2)extract expression between last parenthisis

3)Insert the extracted subexpression between first string after pow and first closing parenthis for all the units.

what i tried :-

 re="pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)+pow((tan),(5))(x)";
    re.split(+);
    re.replaceAll("^[()]]{3}","\\(\\)]*\\)");

to be frank i am new to regular expression.

smraj
  • 152
  • 9
  • 1
    What are you planning to do once you've made your transformation? Do you want to evaluate this? –  Oct 15 '15 at 05:29
  • @torazaburo yes i need to evaluate this – smraj Oct 15 '15 at 05:31
  • Splitting according to +,- ..etc operators won't yeild what you are looking for. It also takes the operators that appears in `4*x+5` like expressions. – Shanavas M Oct 15 '15 at 05:34
  • @shanavasm ,so how can i split that.so should i split based on the operators between ) and pow – smraj Oct 15 '15 at 05:38

3 Answers3

6

If your plan is to evaluate this, rather than lexically transform it, then you can do it by defining appropriate functions:

function pow(operator, exponent) {
  return function(operand) {
    return Math.pow(operator(operand), exponent);
  };
}

var sin = Math.sin, cos = Math.cos, tan = Math.tan;
var x = 2;

> pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)
< 0.17654033709528213
  • how come 0.17654033709528213 be its value when x is given as a general value.You need to substitue any numeric value at place of x to get the value.i am confused by the way javascript handles this. – smraj Oct 15 '15 at 06:01
  • Evaluating it and creating a transformed lexical form are two different things. This solution evaluates it, for a specific value of `x`. If you want the transformed lexical form, to pass to someone else to evaluate or treat as a formula to graph etc., this solution won't work. You'll need to either use a regexp--not a pleasant idea--or write a little parser yourself, which shouldn't be that hard, or maybe someone else has a brilliant idea. –  Oct 15 '15 at 06:07
  • you are absolutely correct torazaburo i need to make a graph from this equation.I need the transformed lexical form.so that i can pass to python script and get an array of value for a particular range and plot it using a javascript charting library – smraj Oct 15 '15 at 06:16
0

You can use following regex and replacement string:

Edited

Regular expression : ([a-z]+)\(\(([a-z]+)\),\(([0-9]+)\)\)\(([a-z0-9\*\+]+)\)([\*\-%\/+]*)

Replacement expression : $1($2($4),($3))$5

Check following code:

function cal()
{
  var string = "pow((sin),(2))(4*x+5)+pow((cos),(3))(4*x+3)/pow((tan),(5))(x)";
  var regex = /([a-z]+)\(\(([a-z]+)\),\(([0-9]+)\)\)\(([a-z0-9\*\+]+)\)([\*\-%\/+]*)/;
  var replacement = "$1($2($4),($3))$5";
  while(string.match(regex))
    {
      string = string.replace(regex,replacement);
    }
  alert(string);
}
cal();
Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • @ chandil03 can you make your answer for more generalised expression. suppose i am giving input as pow((sin),(2))(4*x+5) then also i should get expression pow((sin(4*x+5)),(2))as output. – smraj Oct 15 '15 at 06:03
  • you can up vote my ans if it helped. don't know why this ans got 2 down vote. – Sachin Chandil Oct 15 '15 at 09:48
  • i have upvoted it already.But i need to get some more points to reflect it in your side.thanks for your help – smraj Oct 15 '15 at 11:14
-1

I think regular expression is wrong tool for dealing with this. You may parse the expression in a loop and get the inner expression strings.

Restructure it the way you want and evaluate.

Parsing algorithm could be something like Regular expression to detect semi-colon terminated C++ for & while loops

Community
  • 1
  • 1
Shanavas M
  • 1,581
  • 1
  • 17
  • 24