linear combination of two-component expressions

Asked by Nico Schlömer

I would like to form the linear combination

   \sum_i \alpha_i x_i

where the x_i are Expressions with two components.
I tried

Phi = Constant([0,0])
for alpha, x in (Alpha, X):
      ??? += alpha * x
      ??? += alpha * x

where X is a list of expressions. I'm not sure what to plug in for "???".
I tried Phi[0]/Phi[1], but got

   TypeError: 'Constant' object does not support item assignment.

which makes sense, too.
Confusingly,

Phi0 = Constant(0)
Phi1 = Constant(0)
for alpha, x in (Alpha, X):
      Phi0 += alpha * x
      Phi1 += alpha * x

works, but I'd like Phi to be contained in one variable for the sake of consistency and plotting (see https://answers.launchpad.net/dolfin/+question/214164).

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Nico Schlömer
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

Try:

Phi = Constant([0,0])
Phi0 = Phi[0]
Phi1 = Phi[1]
for alpha, x in (Alpha, X):
      Phi0 += alpha * x
      Phi1 += alpha * x

Johan
On 11/14/2012 02:06 PM, Nico Schlömer wrote:
> New question #214172 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/214172
>
> I would like to form the linear combination
>
> \sum_i \alpha_i x_i
>
> where the x_i are Expressions with two components.
> I tried
>
> Phi = Constant([0,0])
> for alpha, x in (Alpha, X):
> ??? += alpha * x
> ??? += alpha * x
>
> where X is a list of expressions. I'm not sure what to plug in for "???".
> I tried Phi[0]/Phi[1], but got
>
> TypeError: 'Constant' object does not support item assignment.
>
> which makes sense, too.
> Confusingly,
>
> Phi0 = Constant(0)
> Phi1 = Constant(0)
> for alpha, x in (Alpha, X):
> Phi0 += alpha * x
> Phi1 += alpha * x
>
> works, but I'd like Phi to be contained in one variable for the sake of consistency and plotting (see https://answers.launchpad.net/dolfin/+question/214164).
>

Revision history for this message
Nico Schlömer (nschloe) said :
#2

This isn't throwing an error, but doesn't seem to update Phi either. Phi0 and Phi1 do get updated though.

Revision history for this message
Johan Hake (johan-hake) said :
#3

On Nov 14, 2012 6:06 PM, "Nico Schlömer" <
<email address hidden>> wrote:
>
> Question #214172 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214172
>
> Status: Answered => Open
>
> Nico Schlömer is still having a problem:
> This isn't throwing an error, but doesn't seem to update Phi either.
> Phi0 and Phi1 do get updated though.

Phi is a Constant. Why would you like it to change?

Johan

> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

Revision history for this message
Nico Schlömer (nschloe) said :
#4

Well I would like to form a linear combination of Alpha and X. The basic idea was to go like

  sum = 0
  for ...
     sum += alpha * x

Apparently this isn't the right way to proceed here. What is, though?

Revision history for this message
Johan Hake (johan-hake) said :
#5

On 11/14/2012 07:05 PM, Nico Schlömer wrote:
> Question #214172 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214172
>
> Status: Answered => Open
>
> Nico Schlömer is still having a problem:
> Well I would like to form a linear combination of Alpha and X. The basic
> idea was to go like
>
> sum = 0
> for ...
> sum += alpha * x
>
> Apparently this isn't the right way to proceed here. What is, though?

Didn't what I suggested in the previous post do just that?

If you provide a minimal script, which show what you want to do and
where it fails it is easier to help.

Johan

Revision history for this message
Nico Schlömer (nschloe) said :
#6

Right on.

====================== *snip* ======================
from dolfin import *
# -----------------------------------------------------------------------------
def _main():

    mesh = UnitSquare(10, 10)
    V = FunctionSpace(mesh, 'CG', 1)

    # Works:
    X = [Expression('4*x[0]'),
         Expression('2*x[0]+x[1]'),
         Expression('x[0]+2*x[1]')]
    Alpha = [6.0, 1.4, 4.2]
    phi = Constant(0.0)
    for alpha, x in zip(Alpha, X):
       phi += alpha * x
    File('phi.pvd') << project(phi, V)

    # Doesn't work:
    Y = [Expression('4*x[1]'),
         Expression('2*x[1]+x[0]'),
         Expression('x[1]+2*x[0]')]
    phi = Constant([0.0, 0.0])
    Beta = [1.0, 1.3, 1.5]
    for alpha, beta, x, y in zip(Alpha, Beta, X, Y):
       phi[0] += alpha * x - beta * y
       phi[1] += alpha * y + beta * x
    File('Phi.pvd') << project(phi, V*V)

    return
# -----------------------------------------------------------------------------
if __name__ == '__main__':
  _main()
# -----------------------------------------------------------------------------
====================== *snip* ======================

Revision history for this message
Johan Hake (johan-hake) said :
#7

[snip]

    phi_0 = Constant(0.0)
    phi_1 = Constant(0.0)

    Beta = [1.0, 1.3, 1.5]
    for alpha, beta, x, y in zip(Alpha, Beta, X, Y):
       phi_0 += alpha * x - beta * y
       phi_1 += alpha * y + beta * x
    File('Phi.pvd') << project(as_vector([phi_0, phi_1]), V*V)

Johan

On 11/14/2012 09:06 PM, Nico Schlömer wrote:
> Question #214172 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214172
>
> Status: Answered => Open
>
> Nico Schlömer is still having a problem:
> Right on.
>
> ====================== *snip* ======================
> from dolfin import *
> # -----------------------------------------------------------------------------
> def _main():
>
> mesh = UnitSquare(10, 10)
> V = FunctionSpace(mesh, 'CG', 1)
>
> # Works:
> X = [Expression('4*x[0]'),
> Expression('2*x[0]+x[1]'),
> Expression('x[0]+2*x[1]')]
> Alpha = [6.0, 1.4, 4.2]
> phi = Constant(0.0)
> for alpha, x in zip(Alpha, X):
> phi += alpha * x
> File('phi.pvd') << project(phi, V)
>
> # Doesn't work:
> Y = [Expression('4*x[1]'),
> Expression('2*x[1]+x[0]'),
> Expression('x[1]+2*x[0]')]
> phi = Constant([0.0, 0.0])
> Beta = [1.0, 1.3, 1.5]
> for alpha, beta, x, y in zip(Alpha, Beta, X, Y):
> phi[0] += alpha * x - beta * y
> phi[1] += alpha * y + beta * x
> File('Phi.pvd') << project(phi, V*V)
>
> return
> # -----------------------------------------------------------------------------
> if __name__ == '__main__':
> _main()
> # -----------------------------------------------------------------------------
> ====================== *snip* ======================
>

Revision history for this message
Nico Schlömer (nschloe) said :
#8

I don't know as_vector(), but at least the code doesn't fail.
It doesn't do what I want it to do either though, since the file would contain two time steps with phi_0, phi_1, instead of one with components phi_0, phi_1 (like a vector Expression delivers).

Revision history for this message
Johan Hake (johan-hake) said :
#9

Not sure what you mean. This is Phi.pvd

<?xml version="1.0"?>
<VTKFile type="Collection" version="0.1">
  <Collection>
    <DataSet timestep="0" part="0" file="Phi000000.vtu" />
  </Collection>
</VTKFile>

with one timestep.

Johan

On 11/14/2012 10:06 PM, Nico Schlömer wrote:
> Question #214172 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214172
>
> Status: Answered => Open
>
> Nico Schlömer is still having a problem:
> I don't know as_vector(), but at least the code doesn't fail.
> It doesn't do what I want it to do either though, since the file would contain two time steps with phi_0, phi_1, instead of one with components phi_0, phi_1 (like a vector Expression delivers).
>

Revision history for this message
Nico Schlömer (nschloe) said :
#10

Ah, you're right. (I was looking at the wrong file.)
This settles it then. Thanks!
Btw, is there documentation on as_vector()?

Revision history for this message
Johan Hake (johan-hake) said :
#11

Have a look in the UFL chapter in the FEniCS book.

Johan

On 11/15/2012 08:35 AM, Nico Schlömer wrote:
> Question #214172 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214172
>
> Status: Answered => Solved
>
> Nico Schlömer confirmed that the question is solved:
> Ah, you're right. (I was looking at the wrong file.)
> This settles it then. Thanks!
> Btw, is there documentation on as_vector()?
>