Module for working with Vectors

Post and discuss Lemur Modules.
brianc
Regular
Posts:87
Joined:10 Jan 2012 02:16
Re: Module for working with Vectors

Post by brianc » 08 Dec 2012 20:30

Macciza wrote:Hi Brian
I just meant that add function as '+' was already there, which is at he used then 'wrap'ed to the shorter length - I guess I'm used to MutableArrays . . .
I was not sure if you realised the maths operators +-/* already worked on arrays but at longer length making AB code that much simpler as {array}+{array} is already standard within Lemur, rather then iterating though.
I guess ultimately the fact that it is 'add' as opposed to '+' it probably ok - I guess I just wanted the built-in Lemur array maths operators where not thought to be missing or inapplicabble to arrays.
The code optimisation thing was just that there are the inbuilt functions which may be faster, more so than the one-liner thing
I agree with you that duplication is usually useless (whether add truncates or not). If anything, I like having the functions because I think they add some consistency: if I want the distance between two vectors, I use distance(). If I want the dot product, I use dot(). So if I wanted to add two vectors, I think the first thing I'd think of would be to look for a function that does that. Whatever happens with the function names, I'll be sure to be clear that the arithmetic functions truncate.

Thanks, though, for pushing me to think about this stuff. Since my goal is for modules like this to be useful to more people than just me, it's tremendously helpful to get other perspectives.

Macciza
Regular
Posts:1325
Joined:07 Dec 2011 04:57
Location:Sydney, Australia.

Re: Module for working with Vectors

Post by Macciza » 09 Dec 2012 06:05

Hi All

Aah you edited the post whilst I was writing . . .

The is_subset(a,b) of or whatever it was probably should have been the other way is b part of a to maintain consistency ie array , val
I think it is best to keep everything fairly basic - ie fundamentals So I'm not sure about your change to contains() now
it is now contains all as opposed to contains any - and is that contains in order or not ?
Will have a bit of a look at what changes you made later

Also there may be possible variation on the equation that are far easier - I just need to check a few things first

MM
iMac 2.8G i7 12G 10.6.8/10.7.2, Legacy Dexter/Lemur, Liine Lemur/iPad2, KMI SoftStep, 12Step & QuNeo , B-Controls, Mackie C4 etc
MaxMSP, Live Suite, Native Instrument stuff, etc Modified Virtual Guitar System etc All Projects/Modules © CC-BY-NC-SA[*][/b]

m127
Newbie
Posts:36
Joined:14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 » 09 Dec 2012 08:14

Hey Brianc

yeah, this forum does not subscribe you automatically, you have to edit the settings in the Control Panel
Same happened to me on many threads before this one.

I see what you mean about contains()

Suggestion: I still think it is useful, though I suggest you clarify what exactly the outcome is based on. Apparently, this is like this by default in Lemur/programming environments. I was banging my head because, to me, whether a vector or a singleton, a value should not be validated as "contained" unless the exact value or series of values are in fact contained. But this is not a problem really. I was approaching the problem using common logic, instead of programming logic.

good talking to you man, and thanks for your quality reply.

brianc
Regular
Posts:87
Joined:10 Jan 2012 02:16

Re: Module for working with Vectors

Post by brianc » 09 Dec 2012 08:26

Macciza wrote:The is_subset(a,b) of or whatever it was probably should have been the other way is b part of a to maintain consistency ie array , val
I think it is best to keep everything fairly basic - ie fundamentals So I'm not sure about your change to contains() now
it is now contains all as opposed to contains any - and is that contains in order or not ?
Yeah, I figured is_subset made the most sense for what that function does and made contains an alias of that, but I now realize that's not the best approach either. What do you think makes the most sense? contains() could do what it did before and return whether or not a single value is in the given vector? My original idea was to pass it a vector and return which of those is in the other vector, but Lemur doesn't have empty vectors, so if there are no matches, there's a problem.

I was back and forth on the order for the arguments to is_subset(), and I think you're right. I'll switch it.
Macciza wrote:Also there may be possible variation on the equation that are far easier - I just need to check a few things first
Excellent. Now that documentation will primarily go elsewhere, I'm ok with making the code more concise now. I'll start condensing things, and I'd be happy to take suggestions.

brianc
Regular
Posts:87
Joined:10 Jan 2012 02:16

Re: Module for working with Vectors

Post by brianc » 09 Dec 2012 08:33

m127 wrote:Suggestion: I still think it is useful, though I suggest you clarify what exactly the outcome is based on. Apparently, this is like this by default in Lemur/programming environments. I was banging my head because, to me, whether a vector or a singleton, a value should not be validated as "contained" unless the exact value or series of values are in fact contained. But this is not a problem really. I was approaching the problem using common logic, instead of programming logic.
Sorry, just to make sure I get what your perspective was: by "exact value or series of values" do you mean the ordering as well? Like {1,2,3,4,5} contains {1,2,3}, but not {3,2,1}? I suppose by default I look at a vector as a set of values and that ordering doesn't matter, but that's definitely not always the case.

m127
Newbie
Posts:36
Joined:14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 » 09 Dec 2012 08:49

brianc wrote:
m127 wrote:Suggestion: I still think it is useful, though I suggest you clarify what exactly the outcome is based on. Apparently, this is like this by default in Lemur/programming environments. I was banging my head because, to me, whether a vector or a singleton, a value should not be validated as "contained" unless the exact value or series of values are in fact contained. But this is not a problem really. I was approaching the problem using common logic, instead of programming logic.
Sorry, just to make sure I get what your perspective was: by "exact value or series of values" do you mean the ordering as well? Like {1,2,3,4,5} contains {1,2,3}, but not {3,2,1}? I suppose by default I look at a vector as a set of values and that ordering doesn't matter, but that's definitely not always the case.
No, in my case scenario, order was irrelevant (though it is relevant strictly speaking).

If you take a close look at my "solution" script and probably even make an example of your own, you will see how variable 'a' gets filled with 1's and 0's on every loop pass (true or false respectively).
Since we set 'i' to 0 since the beginning, I think arrays are checked in order starting from 0 anyway.

What I was finding amusing, and also was contributing to my pulling my hair, was that in Lemur, apparently (only apparently, not sure), the order does not matter either.


The point of the matter is though that the manual says

==

means EQUAL TO.


I might be crazy, but to me: vectorA is NOT equal to vector B if vectorA is {1,2,3,4,5} and vectorB is {2,3,4}; however, to Lemur those two are equal. Have you tried this?

do something with those same values like:

if (vectorA==vectorB){something;}







.

m127
Newbie
Posts:36
Joined:14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 » 09 Dec 2012 09:03

further, try something like:

vectorA={1,2,3,4,5}
vectorB={3,90,4}

then

if (vectorA==vectorB){something;}


You will get a "true" from that.


why is so?

m127
Newbie
Posts:36
Joined:14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 » 09 Dec 2012 09:07

it looks more like

==

means really

"any == any"

Macciza
Regular
Posts:1325
Joined:07 Dec 2011 04:57
Location:Sydney, Australia.

Re: Module for working with Vectors

Post by Macciza » 09 Dec 2012 11:21

Hi again
A few comments . . .
I will also alert some of the devs to this thread to offer official clarification
Put {1,2,3,4,5}=={1,2,3} as the value for a Monitor and what do you get? That is the key here,
you are not actually comparing the arrays as such but are in fact comparing each index, and carrying any excess indexs (indices) - I don't think of them as padded even though in maths they can be viewed that way . . .
So taking that into account and refactoring - if (wrap(vectorB==vectorA,sizeof(vectorB))){ } which is correct for what it is doing, the first three vals don't match each other . . .

So as you can see 'order is important' - also it may be that because the array is being reduced to a 0/1 by the 'if' that only the first value is relevant in the end . . .

Anyway another option for a 'contains_val(array,val)' function but still not 'array-safe' but easily iterated - firstof(array/val==1)<sizeof(array)?1:0

Also Brian, remember angle() already exists as a builtin function - in fact due to many of them being pretty standard words they may be used at sometime in the standard Lemur library . . .
Also for optimisation notice that append and prepend are both just versions of {{A},{B}} -but reordered . . .

Cheers
MM
iMac 2.8G i7 12G 10.6.8/10.7.2, Legacy Dexter/Lemur, Liine Lemur/iPad2, KMI SoftStep, 12Step & QuNeo , B-Controls, Mackie C4 etc
MaxMSP, Live Suite, Native Instrument stuff, etc Modified Virtual Guitar System etc All Projects/Modules © CC-BY-NC-SA[*][/b]

brianc
Regular
Posts:87
Joined:10 Jan 2012 02:16

Re: Module for working with Vectors

Post by brianc » 09 Dec 2012 18:41

Just a quick suggestion about how you can see if two vectors are exactly equal (same values, same order). You can do something like:

Code: Select all

SLVector.distance(vec1,vec2)==0 && sizeof(vec1)==sizeof(vec2)
That checks to see if the distance between the two vectors is 0 and that they're the same size (to make sure truncation isn't an issue). In "pure" Lemur, you can make a function to compare two input vectors a and b:

Code: Select all

decl i, lenA=sizeof(a), lenB=sizeof(b);

if(lenA != lenB) return 0;

for(i=0; i<lenA; i++) if(a[i] != b[i]) return 0;

return 1;

Post Reply