converts a gmp_complex to a string ( + I * )
| 694 | // converts a gmp_complex to a string ( <real part> + I * <imaginary part> ) |
| 695 | // |
| 696 | char *complexToStr( gmp_complex & c, const unsigned int oprec, const coeffs src ) |
| 697 | { |
| 698 | const char * complex_parameter = "I"; |
| 699 | int N = 1; // strlen(complex_parameter); |
| 700 | |
| 701 | if (nCoeff_is_long_C(src)) |
| 702 | { |
| 703 | complex_parameter = n_ParameterNames(src)[0]; |
| 704 | N = strlen(complex_parameter); |
| 705 | } |
| 706 | |
| 707 | assume( complex_parameter != NULL && N > 0); |
| 708 | |
| 709 | char *out,*in_imag,*in_real; |
| 710 | |
| 711 | c.SmallToZero(); |
| 712 | if ( !c.imag().isZero() ) |
| 713 | { |
| 714 | |
| 715 | in_real=floatToStr( c.real(), oprec ); // get real part |
| 716 | in_imag=floatToStr( abs(c.imag()), oprec ); // get imaginary part |
| 717 | |
| 718 | if (nCoeff_is_long_C(src)) |
| 719 | { |
| 720 | int len=(strlen(in_real)+strlen(in_imag)+7+N)*sizeof(char); |
| 721 | out=(char*)omAlloc(len); |
| 722 | memset(out,0,len); |
| 723 | if ( !c.real().isZero() ) // (-23-i*5.43) or (15.1+i*5.3) |
| 724 | sprintf(out,"(%s%s%s*%s)",in_real,c.imag().sign()>=0?"+":"-",complex_parameter,in_imag); |
| 725 | else // (-i*43) or (i*34) |
| 726 | { |
| 727 | if (c.imag().isOne()) |
| 728 | sprintf(out,"%s", complex_parameter); |
| 729 | else if (c.imag().isMOne()) |
| 730 | sprintf(out,"-%s", complex_parameter); |
| 731 | else |
| 732 | sprintf(out,"(%s%s*%s)",c.imag().sign()>=0?"":"-", complex_parameter,in_imag); |
| 733 | } |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | int len=(strlen(in_real)+strlen(in_imag)+9) * sizeof(char); |
| 738 | out=(char*)omAlloc( len ); |
| 739 | memset(out,0,len); |
| 740 | if ( !c.real().isZero() ) |
| 741 | sprintf(out,"(%s%s%s)",in_real,c.imag().sign()>=0?"+I*":"-I*",in_imag); |
| 742 | else |
| 743 | sprintf(out,"(%s%s)",c.imag().sign()>=0?"I*":"-I*",in_imag); |
| 744 | } |
| 745 | omFree( (void *) in_real ); |
| 746 | omFree( (void *) in_imag ); |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | out= floatToStr( c.real(), oprec ); |
| 751 | } |
| 752 | |
| 753 | return out; |
no test coverage detected