PullMonkey Blog


26 Mar

ECE231 - Spring 2009 - Programming Assignment 3


This is a step-by-step tutorial for this assignment and an explanation of the basics of how it works for those that are having a difficult time understanding or just are stuck somewhere. To view the assignment click here.

So for this assignment we are going to be doing a lot of math calculations so we are going to need the include the both the iostream and the cmath libraries. We will also need the value of pi.

1
2
3
4
5
6
7
8
9

#include <iostream>
#include <cmath>
using namespace std;

#define PI 3.141592654
or
const double PI = 3.141592654;

Defining the Complexn Class

For the complexn class we need 2 private variables r and i, 3 constructors, a copy constructor, 4 member functions, 9 overloaded opperators, and 2 friend functions.

Like the previous program we will define everything with in the class and have the actual functions at the bottom.

So here is what your class should look like (keep in mind that this all goes at the top before the main function):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

class complexn
{
  private:
    double r; // real part of the complex number
    double i; // imaginary part of the complex number
  public:
    complexn();
    complexn(double );
    complexn(double , double );
    complexn(const complexn & );

    double complexabs();
    double complexangle();
    complexn complexconj();
    double distance(const complexn & );

    complexn operator + (const complexn & );
    complexn operator - (const complexn & );
    complexn operator * (const complexn & );
    complexn operator / (const complexn & );
    complexn operator = (const complexn & );

    complexn operator ++ ();
    complexn operator ++ (int );
    complexn operator -- ();
    complexn operator -- (int );

    friend ostream & operator << (ostream & , const complexn & );
    friend istream & operator >> (istream & , complexn & );
};

Writing the Actual Functions for the Complexn Class

First we have the 3 constructors and the copy constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

// takes no arguments and sets r and i to 0.0
complexn::complexn()
{
  r = i = 0.0;
}
// takes 1 argument and sets r to the argument and i to 0.0
complexn::complexn(double real)
{
  r = real;
  i = 0.0;
}
// takes 2 arguments and sets r to the first and i to the second
complexn::complexn(double real, double imag)
{
  r = real;
  i = imag;
}
// this is a copy constructor that dereferences the complexn variable if it is referenced
complexn::complexn(const complexn &c)
{
  r = c.r;
  i = c.i;
}

Next, we have the 3 member functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// takes no arguments and returns the distance from the 0 as a double
double complexn::complexabs()
{
  return sqrt(pow(r,2.0) + pow(i,2.0));
}
// takes no arguments and returns the angle of the complex point as a double in radians
double complexn::complexangle()
{
  double angle; // angle of coordinate from positive x axis
  angle = atan(r / i);
  if(r < 0 and i >= 0)
    return PI - abs(angle);
  if(r < 0 and i < 0)
    return -(PI - abs(angle));
  return angle;
}
// takes no arguments and returns the conjugate of the complex number as a complexn class
complexn complexn::complexconj()
{
  complexn temp = *this; // complex number used for calculations
  temp.i *= -1;
  return temp;
}
// takes one complexn type argument and returns the distance between the argument and the current instance
double complexn::distance(const complexn &temp)
{
  return sqrt(pow(temp.r -r, 2.0) + pow(temp.i - i, 2.0));
}

Next, we have the 9 overloaded operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

complexn complexn::operator + (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r + temp.r;
  cn.i = i + temp.i;
  return cn;
}
complexn complexn::operator - (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r - temp.r;
  cn.i = i - temp.i;
  return cn;
}
complexn complexn::operator * (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = r * temp.r - i * temp.i;
  cn.i = r * temp.i + i * temp.r;
  return cn;
}
complexn complexn::operator / (const complexn &temp)
{
  complexn cn; // complex number used for calculations
  cn.r = (r * temp.r + i * temp.i) / (pow(temp.r, 2.0) + pow(temp.i, 2.0));
  cn.i = (i * temp.r - r * temp.i) / (pow(temp.r, 2.0) + pow(temp.i, 2.0));
  return cn;
}
complexn complexn::operator = (const complexn &temp)
{
  r = temp.r;
  i = temp.i;
  return *this;
}
complexn complexn::operator ++ ()
{
  r += 1;
  return *this;
}
complexn complexn::operator ++ (int x)
{
  complexn temp = *this;
  r += 1;
  return temp;
}
complexn complexn::operator -- ()
{
  r -= 1;
  return *this;
}
complexn complexn::operator -- (int x)
{
  complexn temp = *this;
  r -= 1;
  return temp;
}

Last, we have the 2 friend functions:

1
2
3
4
5
6
7
8
9
10
11
12

ostream & operator << (ostream &os, const complexn &temp)
{
  os << temp.r << " + " << temp.i << "i";
  return os;
}
istream & operator >> (istream &is, complexn &temp)
{
  is >> temp.r >> temp.i;
  return is;
}

Testing Your program

I wrote a fairly good program for testing this complexn class.

To test your program copy your program to [name].h file and in the main.cpp file change the header file at the top to match your file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

#include <iostream>
#include "yourprogram.h" // the name of your comlexn header file goes here

using namespace std;

int main()
{
  complexn c1;
  complexn c2(1);
  complexn c3(3, -5);
  complexn c4, c5, c6;

  cout << "Complex Numbers: " << endl;
  cout << "  C1: " << c1 << endl;
  cout << "  C2: " << c2 << endl;
  cout << "  C3: " << c3 << endl;

  cout << "nEnter values for C1: ";
  cin >> c1;
  cout << "  You enetered: " << c1 << endl;
  cout << "Enter values for C2: ";
  cin >> c2;
  cout << "  You enetered: " << c2 << endl;
  cout << "Enter values for C3: ";
  cin >> c3;
  cout << "  You enetered: " << c3 << endl;

  cout << "nDistances from origin: " << endl;
  cout << "  C1(" << c1 << "): " << c1.complexabs() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexabs() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexabs() << endl;

  cout << "nConjugates:" << endl;
  cout << "  C1(" << c1 << "): " << c1.complexconj() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexconj() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexconj() << endl;

  cout << "nDistances between:" << endl;
  cout << "  C1(" << c1 << ") and C2(" << c2 << "): " << c1.distance(c2) << endl;
  cout << "  C1(" << c1 << ") and C3(" << c3 << "): " << c1.distance(c3) << endl;
  cout << "  C2(" << c2 << ") and C3(" << c3 << "): " << c2.distance(c3) << endl;

  cout << "nAddition:" << endl;
  cout << "  C1(" << c1 << ") + C2(" << c2 << "): " << c1 + c2 << endl;
  cout << "  C1(" << c1 << ") + C3(" << c3 << "): " << c1 + c3 << endl;
  cout << "  C2(" << c2 << ") + C3(" << c3 << "): " << c2 + c3 << endl;

  cout << "nSubtraction:" << endl;
  cout << "  C1(" << c1 << ") - C2(" << c2 << "): " << c1 - c2 << endl;
  cout << "  C1(" << c1 << ") - C3(" << c3 << "): " << c1 - c3 << endl;
  cout << "  C2(" << c2 << ") - C3(" << c3 << "): " << c2 - c3 << endl;

  cout << "nMultiplication:" << endl;
  cout << "  C1(" << c1 << ") * C2(" << c2 << "): " << c1 * c2 << endl;
  cout << "  C1(" << c1 << ") * C3(" << c3 << "): " << c1 * c3 << endl;
  cout << "  C2(" << c2 << ") * C3(" << c3 << "): " << c2 * c3 << endl;

  cout << "nDivision:" << endl;
  cout << "  C1(" << c1 << ") / C2(" << c2 << "): " << c1 / c2 << endl;
  cout << "  C1(" << c1 << ") / C3(" << c3 << "): " << c1 / c3 << endl;
  cout << "  C2(" << c2 << ") / C3(" << c3 << "): " << c2 / c3 << endl;

  cout << "nIncrementing:" << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  C1++: " << c1++ << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  ++C1: " << ++c1 << endl;
  cout << "  C1 =  " << c1   << endl;

  cout << "  C2 =  " << c2   << endl;
  cout << "  C2++: " << c2++ << endl;
  cout << "  C2 =  " << c2   << endl;
  cout << "  ++C2: " << ++c2 << endl;
  cout << "  C2 =  " << c2   << endl;

  cout << "  C3 =  " << c3   << endl;
  cout << "  C3++: " << c3++ << endl;
  cout << "  C3 =  " << c3   << endl;
  cout << "  ++C3: " << ++c3 << endl;
  cout << "  C3 =  " << c3   << endl;

  cout << "nDecrementing:" << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  C1--: " << c1-- << endl;
  cout << "  C1 =  " << c1   << endl;
  cout << "  --C1: " << --c1 << endl;
  cout << "  C1 =  " << c1   << endl;

  cout << "  C2 =  " << c2   << endl;
  cout << "  C2--: " << c2-- << endl;
  cout << "  C2 =  " << c2   << endl;
  cout << "  --C2: " << --c2 << endl;
  cout << "  C2 =  " << c2   << endl;

  cout << "  C3 =  " << c3   << endl;
  cout << "  C3--: " << c3-- << endl;
  cout << "  C3 =  " << c3   << endl;
  cout << "  --C3: " << --c3 << endl;
  cout << "  C3 =  " << c3   << endl;

  c4 = c1;
  c5 = c2;
  c6 = c3;

  cout << "nEquals:" << endl;
  cout << "  C4 = C1(" << c1 << "): " << c4 << endl;
  cout << "  C5 = C2(" << c2 << "): " << c5 << endl;
  cout << "  C6 = C3(" << c3 << "): " << c6 << endl;

  c1 = complexn ( 1, 1);
  c2 = complexn (-1, 1);
  c3 = complexn (-1,-1);
  c4 = complexn ( 1,-1);
  c5 = complexn ( 0, 1);
  c6 = complexn (-1, 0);
  cout << "nAngles:" << endl;
  cout << "  C1(" << c1 << "): " << c1.complexangle() << endl;
  cout << "  C2(" << c2 << "): " << c2.complexangle() << endl;
  cout << "  C3(" << c3 << "): " << c3.complexangle() << endl;
  cout << "  C4(" << c4 << "): " << c4.complexangle() << endl;
  cout << "  C5(" << c5 << "): " << c5.complexangle() << endl;
  cout << "  C6(" << c6 << "): " << c6.complexangle() << endl;

  return 0;
}


10 Mar

Compilation failed: this version of PCRE is not compiled with PCRE_UTF8 support


I don't do too much with php these days, and really haven't since I found rails many years ago. But I figured I would give magento a try, just for fun. Most of it worked, but I continually came across this PCRE error - the full error that I go was:


Warning: preg_match_all() [function.preg-match-all]: Compilation failed: this version of PCRE is not compiled with PCRE_UTF8 support at offset 0  ...

Ok, loads and loads of research led no where, or at least no where that I was willing to go. I tried so hard to find a solution that didn't require me recompiling apache ... well no such luck. I am using slicehost's ubuntu 8.04, so I took the dpkg approach to help make things simple.


Well, I am sure you have found out by now that apache comes with its own version of pcre built in (version 5). This version does not come with UTF-8 support. The trick is to tell apache to use an external system package of pcre. This requires the slightest bit of reconfiguring of apache.

Initially, my pcre looked like this (notice the version of 5.0...):



As mentioned earlier this is a pretty good indicator that your pcre is the built-in apache version. This is not good.



Ok, the solution is not too bad. Compared to this article, which I learned a few things from, it is much simpler. One thing, you will note in the article is the use of --with-pcre=/usr - this is quite different than the --with-pcre=yes that apache2 comes configured with from gusty.

So the solution is to make that change by doing this:

1) Get the source code:

1
2
3
mkdir apache_src
cd apache_src/
apt-get source apache2

2) Modify the configuration (specifically the AP2_COMMON_CONFARGS in debian/rules)
Once the source is downloaded you should have several files and a directory. Edit the debian/rules files from within the apache directory. The only change that is needed is to find the line that says --with-pcre=yes and change it to say --with-pcre=/usr.

This, of course, assumes that you installed pcre and it lives in /usr/(bin,lib ...):

1
2
vi apache2-2.2.4/debian/rules # make those changes
./apache2-2.2.4/debian/rules

3) Install build dependencies for apache:


sudo apt-get build-dep apache2

4) Now build the package:

1
2
3
4
cd apache2-2.2.4/
# you may need to install fakeroot for this:
sudo apt-get install fakeroot 
dpkg-buildpackage -rfakeroot -uc -b

5) Install the new apache package:

1
2
cd ..
sudo dpkg -i apache2_2.2.4-3ubuntu0.1_all.deb



Voila, now look what I have:



That should be it.



Good luck.