pelib  2.0.0
src/complex.c
Go to the documentation of this file.
00001 /*
00002  * complex.c
00003  *
00004  *  Created on: 20 Feb 2012
00005  *  Copyright 2012 Nicolas Melot
00006  *
00007  * This file is part of pelib.
00008  * 
00009  *     pelib is free software: you can redistribute it and/or modify
00010  *     it under the terms of the GNU General Public License as published by
00011  *     the Free Software Foundation, either version 3 of the License, or
00012  *     (at your option) any later version.
00013  * 
00014  *     pelib is distributed in the hope that it will be useful,
00015  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *     GNU General Public License for more details.
00018  * 
00019  *     You should have received a copy of the GNU General Public License
00020  *     along with pelib. If not, see <http://www.gnu.org/licenses/>.
00021  * 
00022  */
00023 #include <math.h>
00024 #include <stdlib.h>
00025 
00026 
00027 #include <pelib/complex.h>
00028 
00029 int
00030 pelib_copy
00031 ( complex_t)(complex_t s1, complex_t * s2)
00032 {
00033   s2->r = s1.r;
00034   s2->im = s1.im;
00035 
00036   return 1;
00037 }
00038 
00039 int
00040 pelib_init
00041 ( complex_t)(complex_t *cplx)
00042 {
00043   cplx->im = 0;
00044   cplx->r = 0;
00045 
00046   return 1;
00047 }
00048 
00049 int
00050 pelib_compare
00051 ( complex_t)(complex_t a, complex_t b)
00052 {
00053   if (a.r == b.r)
00054     {
00055       return a.im - b.im;
00056     }
00057   else
00058     {
00059       return a.r - b.r;
00060     }
00061 }
00062 
00063 FILE*
00064 pelib_printf(complex_t)(FILE* stream, complex_t a)
00065 {
00066         char *str = pelib_string(complex_t)(a);
00067         fprintf(stream, "%s\n", str);
00068         free(str);
00069         return stream;
00070 }
00071 
00072 char*
00073 pelib_string(complex_t)(complex_t a)
00074   {
00075     size_t size;
00076     char* str;
00077 
00078     size = (int)(a.r > 0 ? log10(a.r) : 1) + (int)(a.im > 0 ? log10(a.im) : 1) + sizeof(char) * 3;
00079     str = malloc(sizeof(char) * (size + 1));
00080     sprintf(str, "(%i,%i)", a.r, a.im);
00081 
00082     return str;
00083   }
00084 
00085 char*
00086 pelib_string_detail(complex_t)(complex_t a, int level)
00087 {
00088         return pelib_string(complex_t)(a);
00089 }
00090 
00091 size_t
00092 pelib_fread(complex_t)(complex_t* buffer, FILE* stream)
00093 {
00094   size_t total, total_both;
00095   int num;
00096   int read, has_more;
00097   complex_t ret;
00098 
00099   read = 0;
00100   has_more = 1;
00101   num = 0;
00102   total = 0;
00103   total_both = 0;
00104 
00105   // Read real part
00106   while (read != ' ')
00107     {
00108       if ((has_more = fread(&read, sizeof(char), 1, stream)) == 0 || read == ' ')
00109         {
00110           break;
00111         }
00112 
00113       total += has_more;
00114       num = num * 10 + read - '0';
00115     }
00116   ret.r = num;
00117   total_both = total;
00118   read = 0;
00119 
00120   // Read imaginary part
00121   // If cannot read imaginary part, then leave buffer untouched
00122   // And only return the number of characters read for the real part
00123   if (has_more && (has_more = fread(&read, sizeof(char), 1, stream)) != 0)
00124   {
00125     while (read != ' ')
00126     {
00127       if ((has_more = fread(&read, sizeof(char), 1, stream)) == 0 || read == ' ')
00128         {
00129           break;
00130         }
00131 
00132       total += has_more;
00133       num = num * 10 + read - '0';
00134     }
00135 
00136   ret.im = num;
00137   *buffer = ret;
00138   }
00139 
00140   return total_both;
00141 }
00142 
00143 // Now include the generic stack implementation
00144 #define STACK_T complex_t
00145 #include <pelib/stack.c>
00146 #define ARRAY_T complex_t
00147 #include <pelib/array.c>
00148 #define CFIFO_T complex_t
00149 #include <pelib/fifo.c>