By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Symbolic computation : the derivative of a product.

P: n/a
Hello,

This time I try to simulate the derivative of a product.

Into a text file,
I have an equation of this type.
I have created these equations
with the previous functions.

Examples:

eq01.txt:
@[a*cos(2*x+3*x^2)*cos(2*n+3*a^2)*b*cos(2*x+3*a^2)]

eq02.txt:
@[a*cos(x)*cos(a)*sin(x)]

eq03.txt: (try this first)
@[a*g(b)*f(x)*c*h(d)*h(x)*i(x)*k(x)]
The functions
=============

D_product_a();

copy into "eqconst" the constant function.
copy into "eqx" the function which depend of x.

strcat_D();

write the derivative of the function (simulate) (sign @)

D_product();

Call the two previous functions,
to do the work.
See below my work.

Have I do some mistakes?

The result seem correct.

Thank.

/* ------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* ------------------------------------ */
#define CHAR_MAX 1000
#define CHAR_A 2
/* ------------------------------------ */
void strcat_D(char *eq,char *eqx,int i);
int D_product_a(char *filename,char *eqx,char *eqconst,char *x);
void D_product(char *filename,char *eq,char *x);
/* ------------------------------------ */
/* ------------------------------------ */
int main(void)
{
char filename[FILENAME_MAX];
char eq[CHAR_MAX];
char x[CHAR_A];

strcpy(filename,"eq03.txt");
strcpy(x,"x");

D_product(filename,eq,x);

printf("\n\n%s \n\n",eq);

getchar();
return 0;
}
/* ------------------------------------ */
/* ------------------------------------ */
int D_product_a(
char *filename,
char *eqx,
char *eqconst,
char *x
)
{
/* ------------------------------------ */
FILE *f;
/* ------------------------------------ */
char t[CHAR_MAX];
char c[CHAR_A];
/* ------------------------------------ */
int c_int;
int p;
int y;
int n_product;
/* ------------------------------------ */
strcpy( t,"");
strcpy( c,"");
strcpy(eqx,"");
/* ------------------------------------ */
p=0;
y=0;
n_product=0;
/* ------------------------------------ */

f = fopen(filename,"r");

if (f==NULL) printf("File doesn't exist\n");
else
{
while( (c_int=getc(f))!=EOF )
{
c[0]=c_int;
if( !(strcmp(c,"\n"))||
!(strcmp(c," " ))||
!(strcmp(c,"@" ))||
!(strcmp(c,"[" )) )

strcpy(c,"");

else{
if( !(strcmp(c,"(")) ) p++;
else if( !(strcmp(c,")")) ) p--;
else if( !(strcmp(c,x )) ) y=1;

if( (!strcmp(c,"*")||
!strcmp(c,"]"))&&
!p )/* p==0 true */
{
if(y==0)
{
strcat(eqconst,t);
strcpy(t,"");
}
else
{
t[0]=' ';
strcat(eqx," ");
strcat(eqx,"[");
strcat(eqx,t);
strcat(eqx,"]");
strcpy(t,"");
++n_product;
y=0;
}
}
strcat(t,c);
}
}
}

if(f!=NULL)fclose(f);

return(n_product);
}

/* ------------------------------------ */
void strcat_D(
char *eq,
char *eqx,
int i)
{
/* ------------------------------------ */
int n;
/* ------------------------------------ */
n=1;
/* ------------------------------------ */

for (;*eq;++eq);

while ((*eqx++))
{
if( (*eqx =='['))
{
if(n==i)*eq++ ='@';
else *eq++ =' ';
n++;
}
*eq++ = *eqx;
}
}

/* ------------------------------------ */
void D_product(
char *filename,
char *eq,
char *x
)
{
/* ------------------------------------ */
char t [CHAR_MAX];
char eqx [CHAR_MAX];
char eqconst[CHAR_MAX];
/* ------------------------------------ */
int i;
/* ------------------------------------ */

strcpy(eq,"");

for(i=D_product_a(filename,eqx,eqconst,x); i;i--)
{
strcat(eq,"[");
strcat(eq,eqconst);
strcat(eq,"]");
strcat_D(eq,eqx,i);
if(i>1)strcat(eq,"+\n");
}
}

Mar 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Here is one problem:
#define CHAR_MAX 1000
>From limits.h, this is a C language macro:
- maximum value for an object of type char
CHAR_MAX

You should choose another name.

Mar 9 '07 #2

P: n/a
On Mar 9, 12:14 pm, "xhunga" <xhun...@yahoo.frwrote:
#include <string.h>
{snip}
void strcat_D(
Here's another thing.
Function names starting with str* belong to the implementation.
Call it something like myStrcat().

Mar 9 '07 #3

P: n/a

user923005 a écrit :
On Mar 9, 12:14 pm, "xhunga" <xhun...@yahoo.frwrote:
#include <string.h>
{snip}
void strcat_D(

Here's another thing.
Function names starting with str* belong to the implementation.
Call it something like myStrcat().
thank for your help.

Mar 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.