# Affine transformation: Translation X-Axis of Function

**URL:** <https://fenicsproject.discourse.group/t/affine-transformation-translation-x-axis-of-function/3438>\
**Category:** Uncategorized\
**Created:** [June 4, 2020, 7:41pm UTC](https://fenicsproject.discourse.group/t/affine-transformation-translation-x-axis-of-function/3438 "2020-06-04T19:41:06Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![thabuis](https://avatars.discourse-cdn.com/v4/letter/t/5f8ce5/32.png) [@thabuis](https://fenicsproject.discourse.group/u/thabuis)\
**Post date:** [June 24, 2020, 1:27pm UTC](https://fenicsproject.discourse.group/t/affine-transformation-translation-x-axis-of-function/3438/9 "2020-06-24T13:27:44Z")

</div>

How can this operation be accounted for while computing the derivative with **dolfin\_adjoint**?

I get the following error: _AttributeError: ‘float’ object has no attribute ‘\_cpp\_object’_  
I tried to see how it is commonly done when defining UserExpression as shown in [here](http://www.dolfin-adjoint.org/en/release/documentation/time-dependent-wave/time-dependent-wave.html), but cannot manage to adapt it to my problem …

Here follows a minimal non-working example to show what is desired. You can comment the lines after the plots to have it running without error:

```auto
from dolfin import * 
from dolfin_adjoint import *

import matplotlib.pyplot as plt
import numpy as np

lx = 1.0
xmin=0.0
xmax=1.0

mesh = UnitSquareMesh(16,16)
V = FunctionSpace(mesh,'CG', 1)

u_0 = interpolate(Expression("x[0]",degree=3), V)

# Shift spatially along x the dofs
b = lx/2.0 #shift along x-axis

class ShiftedExpr(UserExpression):
    def __init__ (self,func,**kwargs):
        super(). __init__ (**kwargs)
        self.func = func
    def eval(self,values,x):
        x0_shift = x[0] - b
        if(x0_shift < xmin):
            x0_shift += lx
        x_shift = np.array([x0_shift,x[1]])
        values[0] = self.func(x_shift)
    def value_shape(self):
        return ()
    
u_1 = project(ShiftedExpr(u_0),V)

pl, ax = plt.subplots();
fig = plt.gcf()
fig.set_size_inches(16, 4)
plt.subplot(1, 2, 1); p = plot(u_0, title='Initial distribution', mode='color',vmin=0.0,vmax=1.0)
p.set_cmap("viridis"); cbar = plt.colorbar(p); # add a colorbar
plt.subplot(1, 2, 2); p = plot(u_1, title='Shifted distribution', mode='color',vmin=0.0,vmax=1.0)
p.set_cmap("viridis"); cbar = plt.colorbar(p); # add a colorbar

# Comment the following to have it running without error
m0 = Control(u_0)
J = -assemble((u_1**2) *dx)**(1/2) # Random Functional to serve as objective function
Jhat = ReducedFunctional(J,m0)
dJdmh0 = Jhat.derivative()

# Taylor remainder test to verify the computation of the gradient
h = Function(V)
h.vector()[:] = 0.01*(1.0- 0.0)
conv_rate = taylor_test(Jhat,u_0,h) #Uses the previously setted 

```

Thank you for your help.

---

_[View the full topic](https://fenicsproject.discourse.group/t/affine-transformation-translation-x-axis-of-function/3438)._
