cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

SPRstructur
15-Moonstone

Matric

Dear all

how to make multiple element by element and result as matrix? (Function As(DB,n,i)
Thank you

phearunstructur_1-1587800667356.png

 

phearunstructur_0-1587800556957.png

 

1 ACCEPTED SOLUTION

Accepted Solutions

Here is an attempt from what I understood so far.

Vectorization as suggested by Terry sure is the clue, but its not so straightforward as you may think because we don't want to vectorize the elements of DB or n but just the elements of the results of the function n.s1() and A.sDB().

Here is one possible way to do it:

Werner_E_0-1587827996036.png

 

Additional remark: Its not necessary to vectorize the call A.sDB(DB) because the function uses DB^2 and squaring a vector is not defined in mathematics. So Prime automatically applies vectorization.
But in my opinion its good habit to apply the vectorization explicitly  yourself in cases like this - just to be on the safe side in case you later change the function A.sDB.

 

 

 

View solution in original post

10 REPLIES 10

Hi,

 

Try "vectorisation".

 

On the ribbon at Math | Operators   - lower right hand corner.

 

Cheers 

Terry

Please attach your worksheet.

And state clearly, what exactly you expect as desired result. For this it might be helpful if you change the input matrices DB and n in a way so not all entries are the same value.

Here is an attempt from what I understood so far.

Vectorization as suggested by Terry sure is the clue, but its not so straightforward as you may think because we don't want to vectorize the elements of DB or n but just the elements of the results of the function n.s1() and A.sDB().

Here is one possible way to do it:

Werner_E_0-1587827996036.png

 

Additional remark: Its not necessary to vectorize the call A.sDB(DB) because the function uses DB^2 and squaring a vector is not defined in mathematics. So Prime automatically applies vectorization.
But in my opinion its good habit to apply the vectorization explicitly  yourself in cases like this - just to be on the safe side in case you later change the function A.sDB.

 

 

 

SPRstructur
15-Moonstone
(To:Werner_E)

Now I am not near my pc. Yep that I prefer but when I enlarge matrix DB 1x4 or higher it will work ?

i will test tomorrow, I will be back

big thank Werner_E &Terry


@SPRstructur wrote:

...

when I enlarge matrix DB 1x4 or higher it will work ?

...

You will have to enlarge the matrix n, too.

As long as DB and n will have the same number of rows, it should work OK without any changes.

 

As I'm in a cat-skinning mood, here's another approach that uses functions to achieve the desired outcome.

 

Element-by-element multiplication is conventionally referred to as Hadamard (or Schur) multiplication.   The attached worksheet implements it as the function hadamard and applies it to your problem.   The worksheet also includes the map, zeros, ones and seq functions.

 

Here are the hadamard and map functions,

 

2020 04 25 A.png

 

And here are the zeros, ones and seq functions.

zeros, ones, and seq functionszeros, ones, and seq functions

 

Stuart

Always nice to see your thought son problems and your consequent use a (useful) custom functions.

 

There is no need for "i" anyway. Neither as range, nor as argument in function "A.s".

 

StuartBruff
23-Emerald II
(To:Werner_E)

Vielen Dank, Werner. I'm pretty sure any one of a number of the "core" Mathcadders (yourself included) would have come up with a similar solution if you too came late to the party! 😎

 

Working with Prime Express somewhat forces a purer functional mindset. If I could find nice (ie, quick and short), non-programming ways to return an array after setting an element and to convert an n-parameter function into an n-1 parameter function, I'd be happy. 

 

Well, I would be if the recursion depth wasn't so shallow, although that in itself is just one more challenge to work around.   The lack of expression wrap-around is also a limiting factor as far as readability goes.

 

Sowieso zurück zur Geschichte, yes, I wondered about the 'i' and guessed it might have been intended for some future use when the OP got the function working.

 

Stuart

LucMeekes
23-Emerald III
(To:StuartBruff)

Nice, Stuart,

 

However there's no need to define the _one() and _zero() functions, see below:

LucMeekes_0-1587906686178.png

Caution: With the 'min' function, you have to set the label of it to 'function'. Prime doesn't understand simple things...

 

Luc

StuartBruff
23-Emerald II
(To:LucMeekes)


@LucMeekes wrote:

Nice, Stuart,

 

However there's no need to define the _one() and _zero() functions, see below:

LucMeekes_0-1587906686178.png

Caution: With the 'min' function, you have to set the label of it to 'function'. Prime doesn't understand simple things...

 

Luc


Hi, Luc.

 

Yes, I've been bitten a few times by forgetting that Prime treats min a bit differently. 

 

The reason I use zero and one is simply to avoid having to do further operations on larger matrices ... I grew up in an era when milliseconds counted and it paid to be familiar with the number of clock cycles the processor took to perform an operation! 

 

The real nuisance is not being able to use partial application in Prime.   What I'd have done (and did do) in M15 would be to write a function like matseq(m,i,j) := i + m * j and then define a function matint(m,n) := matrix(m,n,matseq(m)) to generate a matrix of successive integers, increasing in normal Mathcad order.

 

Unfortunately, I can't think of a neat way of doing this in Prime Express (it would be straightforward in Prime Premium, of course, by defining a local function matrix(m,n,l(i,j)<-matseq(m,i,j)).  The best I've been able to come up with is a double-matrix addition function, and a recursive replicator-adder.  On the other hand, they both seem to execute as quickly as the matrix function alone, with the edge going to the recursive function for large matrices.

 

Anyway, enough rambling, here's a plot of the times for the two approaches to generating an all-ones matrix:

 

2020 04 29 A.png

 

Stuart

Top Tags