Function matrix/vector assembly

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Function matrix/vector assembly

Grant Stephens
Good Morning

I am trying to assemble a function into a matrix or a vector but I have not found a way to do so except for typing it out manually, which is not possible as the it would be around 100 long and wide and changes after every timestep. The following represents what I am trying to do:

f=@(x) [x^2 x 1]
then I want to put a number of these into a vector so that the final vector would look like:
vect=@(x) [x^2 x+x^2 1+x 1]
Here I have added have done something like:
vect=@(x) [f(x)(1) f(x)(2)+f(x)(1) f(x)(3)+f(x)(2) +f(x)(3)]
So basically having a vector with f in 1 to 3 added to f in positions 2 to 4. Obviously this would have to run in a loop as it is well over 100 wide.
Does anybody have any ideas? Am I missing something simple?

Thank you.


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Function matrix/vector assembly

marco atzeri-2
On 8/9/2012 6:48 AM, Grant Stephens wrote:

> Good Morning
>
> I am trying to assemble a function into a matrix or a vector but I have
> not found a way to do so except for typing it out manually, which is not
> possible as the it would be around 100 long and wide and changes after
> every timestep. The following represents what I am trying to do:
>
> f=@(x) [x^2 x 1]
> then I want to put a number of these into a vector so that the final
> vector would look like:
> vect=@(x) [x^2 x+x^2 1+x 1]
> Here I have added have done something like:
> vect=@(x) [f(x)(1) f(x)(2)+f(x)(1) f(x)(3)+f(x)(2) +f(x)(3)]
> So basically having a vector with f in 1 to 3 added to f in positions 2
> to 4. Obviously this would have to run in a loop as it is well over 100
> wide.

Not clear to have understood you need, something like this ?

octave:1> a=1:10
a =

     1    2    3    4    5    6    7    8    9   10

octave:2> a(1:end-1)
ans =

    1   2   3   4   5   6   7   8   9

octave:3> a(2:end)
ans =

     2    3    4    5    6    7    8    9   10

octave:4> a(1:end-1)+a(2:end)
ans =

     3    5    7    9   11   13   15   17   19


> Does anybody have any ideas? Am I missing something simple?
>
> Thank you.
>
Marco


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Function matrix/vector assembly

Francesco Potortì
In reply to this post by Grant Stephens
>I am trying to assemble a function into a matrix or a vector but I have not
>found a way to do so except for typing it out manually, which is not
>possible as the it would be around 100 long and wide and changes after
>every timestep. The following represents what I am trying to do:
>
>f=@(x) [x^2 x 1]
>then I want to put a number of these into a vector so that the final vector
>would look like:
>vect=@(x) [x^2 x+x^2 1+x 1]
>Here I have added have done something like:
>vect=@(x) [f(x)(1) f(x)(2)+f(x)(1) f(x)(3)+f(x)(2) +f(x)(3)]
>So basically having a vector with f in 1 to 3 added to f in positions 2 to
>4. Obviously this would have to run in a loop as it is well over 100 wide.

As far as I am concerned, you should try to explain it better: I read it
carefully, but could nt make out what you need.

--
Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058 (op.2111)
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Function matrix/vector assembly

Juan Pablo Carbajal
On Thu, Aug 9, 2012 at 8:59 AM, Francesco Potortì <[hidden email]> wrote:

>>I am trying to assemble a function into a matrix or a vector but I have not
>>found a way to do so except for typing it out manually, which is not
>>possible as the it would be around 100 long and wide and changes after
>>every timestep. The following represents what I am trying to do:
>>
>>f=@(x) [x^2 x 1]
>>then I want to put a number of these into a vector so that the final vector
>>would look like:
>>vect=@(x) [x^2 x+x^2 1+x 1]
>>Here I have added have done something like:
>>vect=@(x) [f(x)(1) f(x)(2)+f(x)(1) f(x)(3)+f(x)(2) +f(x)(3)]
>>So basically having a vector with f in 1 to 3 added to f in positions 2 to
>>4. Obviously this would have to run in a loop as it is well over 100 wide.
>
> As far as I am concerned, you should try to explain it better: I read it
> carefully, but could nt make out what you need.
>
> --
> Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058 (op.2111)
> ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
> via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040
> (entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave

I am not sure I understood.
What I get is the following: you want to define vector-valued
functions and then be able to operate on those functions (add them,
multiply them, etc).

I do not think there is built in operator for anonymous handles, that is [1]
f  = @(x) x.^2
g = @(x) x
h = f + g

and h should be the anonymous function accepting two input argument such that
h(x,y) = f(x) + g(y)

To emulate this, afaik you have to do it manually (and it wouldn't be efficient)
h = @(x,y) f(x) + g(y)

Now when you call h, you will be then calling f and g, and since
function evaluations are not the most efficient thing, this get worse
themore function you nest.

@John | Jordi: Can [1], i.e. operators between anonymous functions, be
implemented efficiently?
--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Function matrix/vector assembly

Grant Stephens
Ok, let me try and explain it better
I have two functions, say f(x) and g(x), which give vectors as outputs, so something like:
f=@(x) [x^2 x^3]
g=@(x) [x 5*x]
and these must make up a vector function with combinations of f and g, for example:
[x^2 (x^3)+(x) 5(x)]. So in other words the first two values must be from f and added to these in the spaces two and 3 must be the values from g.

Is that clearer?



On 9 August 2012 13:38, Juan Pablo Carbajal <[hidden email]> wrote:
On Thu, Aug 9, 2012 at 8:59 AM, Francesco Potortì <[hidden email]> wrote:
>>I am trying to assemble a function into a matrix or a vector but I have not
>>found a way to do so except for typing it out manually, which is not
>>possible as the it would be around 100 long and wide and changes after
>>every timestep. The following represents what I am trying to do:
>>
>>f=@(x) [x^2 x 1]
>>then I want to put a number of these into a vector so that the final vector
>>would look like:
>>vect=@(x) [x^2 x+x^2 1+x 1]
>>Here I have added have done something like:
>>vect=@(x) [f(x)(1) f(x)(2)+f(x)(1) f(x)(3)+f(x)(2) +f(x)(3)]
>>So basically having a vector with f in 1 to 3 added to f in positions 2 to
>>4. Obviously this would have to run in a loop as it is well over 100 wide.
>
> As far as I am concerned, you should try to explain it better: I read it
> carefully, but could nt make out what you need.
>
> --
> Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058 (op.2111)
> ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
> via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040
> (entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave

I am not sure I understood.
What I get is the following: you want to define vector-valued
functions and then be able to operate on those functions (add them,
multiply them, etc).

I do not think there is built in operator for anonymous handles, that is [1]
f  = @(x) x.^2
g = @(x) x
h = f + g

and h should be the anonymous function accepting two input argument such that
h(x,y) = f(x) + g(y)

To emulate this, afaik you have to do it manually (and it wouldn't be efficient)
h = @(x,y) f(x) + g(y)

Now when you call h, you will be then calling f and g, and since
function evaluations are not the most efficient thing, this get worse
themore function you nest.

@John | Jordi: Can [1], i.e. operators between anonymous functions, be
implemented efficiently?
--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/


_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Function matrix/vector assembly

Juan Pablo Carbajal
On Thu, Aug 9, 2012 at 2:46 PM, Grant Stephens <[hidden email]> wrote:

> Ok, let me try and explain it better
> I have two functions, say f(x) and g(x), which give vectors as outputs, so
> something like:
> f=@(x) [x^2 x^3]
> g=@(x) [x 5*x]
> and these must make up a vector function with combinations of f and g, for
> example:
> [x^2 (x^3)+(x) 5(x)]. So in other words the first two values must be from f
> and added to these in the spaces two and 3 must be the values from g.
>
> Is that clearer?
>
>
>
> On 9 August 2012 13:38, Juan Pablo Carbajal <[hidden email]> wrote:
>>
>> On Thu, Aug 9, 2012 at 8:59 AM, Francesco Potortì <[hidden email]>
>> wrote:
>> >>I am trying to assemble a function into a matrix or a vector but I have
>> >> not
>> >>found a way to do so except for typing it out manually, which is not
>> >>possible as the it would be around 100 long and wide and changes after
>> >>every timestep. The following represents what I am trying to do:
>> >>
>> >>f=@(x) [x^2 x 1]
>> >>then I want to put a number of these into a vector so that the final
>> >> vector
>> >>would look like:
>> >>vect=@(x) [x^2 x+x^2 1+x 1]
>> >>Here I have added have done something like:
>> >>vect=@(x) [f(x)(1) f(x)(2)+f(x)(1) f(x)(3)+f(x)(2) +f(x)(3)]
>> >>So basically having a vector with f in 1 to 3 added to f in positions 2
>> >> to
>> >>4. Obviously this would have to run in a loop as it is well over 100
>> >> wide.
>> >
>> > As far as I am concerned, you should try to explain it better: I read it
>> > carefully, but could nt make out what you need.
>> >
>> > --
>> > Francesco Potortì (ricercatore)        Voice:  +39.050.315.3058
>> > (op.2111)
>> > ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
>> > via G. Moruzzi 1, I-56124 Pisa         Fax:    +39.050.315.2040
>> > (entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it
>> > _______________________________________________
>> > Help-octave mailing list
>> > [hidden email]
>> > https://mailman.cae.wisc.edu/listinfo/help-octave
>>
>> I am not sure I understood.
>> What I get is the following: you want to define vector-valued
>> functions and then be able to operate on those functions (add them,
>> multiply them, etc).
>>
>> I do not think there is built in operator for anonymous handles, that is
>> [1]
>> f  = @(x) x.^2
>> g = @(x) x
>> h = f + g
>>
>> and h should be the anonymous function accepting two input argument such
>> that
>> h(x,y) = f(x) + g(y)
>>
>> To emulate this, afaik you have to do it manually (and it wouldn't be
>> efficient)
>> h = @(x,y) f(x) + g(y)
>>
>> Now when you call h, you will be then calling f and g, and since
>> function evaluations are not the most efficient thing, this get worse
>> themore function you nest.
>>
>> @John | Jordi: Can [1], i.e. operators between anonymous functions, be
>> implemented efficiently?
>> --
>> M. Sc. Juan Pablo Carbajal
>> -----
>> PhD Student
>> University of Zürich
>> http://ailab.ifi.uzh.ch/carbajal/
>
>

This is one of many ways to do it.

Make the connectivity matrix that says which element of f(x) must be
combined with the elements of g(x). In your example
[f(x)(1); f(x)(2)+g(x)(1); g(x)(2)] = M * [f(x); g(x)]
M = [1 0 0 0; 0 1 1 0; 0 0 0 1];
(assuming f and g return column vectors)

Then the new function is
h = @(x) M*[f(x); g(x)];

This is the answer I gave you before. Be aware that this is not
efficient if you are then going to use h to build another function in
this way.


--
M. Sc. Juan Pablo Carbajal
-----
PhD Student
University of Zürich
http://ailab.ifi.uzh.ch/carbajal/
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave