# Coding for Fast Copper

While attempting to program the image of a mandelbrot, I unwittingly chose the easy, convenient, yet very slow way of writing my complex number class. Let’s have a look.

The complex number was coded as follows:

```Complex = [
real = 0
imag = 0
super.real = +(super.real: p.real:)
super.imag = +(super.imag: p.imag:)
}
mult = [p] {
r = super.real:
super.real = -( *(r: p.real:) *(super.imag: p.imag:))
super.imag = +( *(r: p.imag:) *(super.imag: p.real:))
}
]```

The problem isn’t obvious. The class is perfectly acceptable for most practical cases, but the problem arises in its usage.

```z = Complex
c = Complex

loop {
# ... #
z = c
# ... #
} # end loop #
```

The important part of the loop is shown. In a loop in the program, there occurs a copy. But the copy isn’t merely of the members “Complex.real” and “Complex.imag”. It’s also a copy of “Complex.add” and “Complex.mult”, which includes their parameters lists and some other data associated with functions. Even if the copy of a member isn’t too slow, we are in fact copying twice as much as we need to. And if we were to add more members to Complex, the copying of Complex would be even slower!

What’s the solution? Unlike C++, we don’t have the luxury of having class definitions in which class methods are only saved once, but we can simulate that structure by making a single object, Complex, that acts on objects created as copies of a special member of Complex. Let’s see this at work:

```Complex = [
data = [ real = 0, imag = 0 ] # Copy me #
_1.real = +( _1.real: _2.real: )
_1.imag = +( _1.imag: _2.imag: )
}
mult = [_1, _2] {
r = _1.real
_1.real = -( *(r: _2.real:) *(_1.imag: _2.imag:) )
_1.imag = +( *(r: _2.imag:) *(_1.imag: _2.real:) )
}
]

z = Complex.data
c = Complex.data

# Example: #
Complex.add(z,c) # z = z + c #
```

In short, in order to speed up Copper code, we make a base class that is never copied as a whole but contains a member that IS copied AND whose structure the other member functions (of the base class) presume when working on arguments given them. That is, the other member functions expect to be given identical copies of the special member.