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 »

Updated to include some more functions for creating random vectors (floats, integers, and unique floats after inspiration from Antonio), calculating magnitude, calculating angle between vectors, changing the range of all values for all elements, and probably something else. Complete documentation to come soon.

My goal for this module is to create a place to go for working with vectors. I don't personally have uses for all of these (like angle between two vectors), but I wouldn't be surprised if someone comes along who does.

To provide some ideas, vectors can be used to store values of some meter over time. For example, if you're watching the output level in a template for Logic, you might want to know if that level has clipped recently. If you only look at the current value, you can only see clips when they happen. Instead, if you look at the levels for the last 10 seconds, you can know if a clip occurred within that time period.

My personal motivation behind this are for a module I've been developing for syncing to an external MIDI clock. I keep track of when the ticks come in (in a vector) to keep an eye on how tempo changes over time. That lets me monitor the quality of the signal (the reason for my Stats module), detect missed ticks, and so on. It also helps me keep a more consistent tempo instead of only going by instantaneous measures, which can vary quite a bit.
Attachments
screenshot.png
screenshot.png (33.19 KiB) Viewed 2934 times
Macciza
Regular
Posts: 1315
Joined: 07 Dec 2011 04:57
Location: Sydney, Australia.

Re: Module for working with Vectors

Post by Macciza »

Cheers AB
That was what I was getting at - that the 'add' function was already there, just implemented differently
Your add_array would drop in replace for SLVector.add - I think I used subarrays() in a similar case . . .
and I do think it is worth adding something that indicates it truncates as opposed to the in built larger length
Same with some of the others - and if using a lot of them then code optimisation could be worth something at point

And at least one is over-riding a built in function - range(a,min,max) is already included and yours seems no different
Which kind of brings up naming conventions, structuring etc Do things still work if setup slightly differently etc


Re; Vector or Array - I think there may be a distinction between vectors in a dimensional (2d,3d etc) sense and arrays in the sense of lists
But I guess it all just linguistics and your particular application . . .
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 »

Macciza wrote:Cheers AB
That was what I was getting at - that the 'add' function was already there, just implemented differently
Your add_array would drop in replace for SLVector.add - I think I used subarrays() in a similar case . . .
and I do think it is worth adding something that indicates it truncates as opposed to the in built larger length
Same with some of the others - and if using a lot of them then code optimisation could be worth something at point
I'm sorry, I'm not following. Both mine and Antonio's truncate. Are you just saying that it should be indicated in the function name? That's understandable. My original hesitation was that someone coming in who is familiar with other environments would probably expect to look for add to be called add. I tried to make my function names match what is used in other languages as best as I could. But, common behavior elsewhere is to not work at all when vectors have different lengths, so I'll make the names more specific. truncate_add(), add_truncate(), add_tr()? Dunno yet. Same will apply for all the other arithmetic functions.

Code optimization is something I'm back-and-forth on in this case. While a lot of these could be one-liners, that cuts down on a lot of people's ability to read and understand them. I did some of each here, and I'm not quite sure which fits the task better.
Macciza wrote: And at least one is over-riding a built in function - range(a,min,max) is already included and yours seems no different
Which kind of brings up naming conventions, structuring etc Do things still work if setup slightly differently etc
I had mistakenly assumed the built-in range only worked with numbers, but I see it also works with arrays. I'll be removing it.
Macciza wrote: Re; Vector or Array - I think there may be a distinction between vectors in a dimensional (2d,3d etc) sense and arrays in the sense of lists
But I guess it all just linguistics and your particular application . . .
From what I was taught when I learned programming, "arrays" typically refer to data structures that store a fixed number of objects, while "vectors" can change in size appropriately. I still think of them that way (so I would call these "arrays"), and most programming languages follow that convention (like C++'s STL), but people still use them interchangeably.
m127
Newbie
Posts: 32
Joined: 14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 »

hey Brianc, this is a great topic.
This can inspire one to develop stuff.
Thanks Macciza for the headsup on this thread


Hey (Brian/all), I was wondering if you would be interested in taking a look at a little problem I have, precisely with vectors.

Long story short, I wrote a script that populates a vector with about 100 different items or values. Let's call it vectorA

I use that vector for comparison purposes and then to determine the properness of certain scripted routines I have been writing.

Right now I am stuck as I can't figure out how to compare another smaller vector (vectorB) with the contents of vectorA.

ie, say,

Code: Select all

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

vectorA==vectorB?=1:0;

What I need is some sort of function to determine whether vectorB's values are CONTAINED in vectorA, and then obtain a true or false result.

Obviously, the length/size of the vectors is oversimplified. As I said, I need to compare vectors with about 100 values vs smaller vectors with about 6 values.

Basically, this question is preventing me from finishing a project that I have been working for a while!!!


help!!!!


Cheeers!
Last edited by m127 on 06 Dec 2012 06:38, edited 1 time in total.
m127
Newbie
Posts: 32
Joined: 14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 »

I just took a deeper look at your functions/scripts Brian.
Particularly at "contains"

You can check how if you monitor:

Code: Select all

contains({1, 2, 3, 4, 5}, {4, 99})
you always get ret=1 even though the two arrays are not really the same.

For you to get ret=0, you have to have entirely distint arrays like so:

Code: Select all

contains({1, 2, 3, 4, 5}, {7, 99})

I wonder if there is a way to write a function to compare vector "content vs content" without having to create loops.
It looks to me like only a loop would do it here (in regards to my scenario)


PS: Btw, what's up with you Brian putting "creative commons licensing" in the file for these somewhat simple functions that are actually public knowledge and common programming techniques especially in other languages and environments?
Just curious. I find it a bit over the top.
m127
Newbie
Posts: 32
Joined: 14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 »

m127 wrote:I just took a deeper look at your functions/scripts Brian.
Particularly at "contains"

You can check how if you monitor:

Code: Select all

contains({1, 2, 3, 4, 5}, {4, 99})
you always get ret=1 even though the two arrays are not really the same.

For you to get ret=0, you have to have entirely distint arrays like so:

Code: Select all

contains({1, 2, 3, 4, 5}, {7, 99})

I wonder if there is a way to write a function to compare vector "content vs content" without having to create loops.
It looks to me like only a loop would do it here (in regards to my scenario)


PS: Btw, what's up with you Brian putting "creative commons licensing" in the file for these somewhat simple functions that are actually public knowledge and common programming techniques especially in other languages and environments?
Just curious. I find it a bit over the top.
...
Macciza
Regular
Posts: 1315
Joined: 07 Dec 2011 04:57
Location: Sydney, Australia.

Re: Module for working with Vectors

Post by Macciza »

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

m127 - Unfortunately I don't think the code deals with the array input . . .
Also do you want to work if the second array numbers are both in the first, or contiguously in the first - Do the 6 in a 100 need to be in order when they occur?

Cheers all
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]
lABl
Lemur Guru
Posts: 269
Joined: 09 Dec 2011 15:56
Contact:

Re: Module for working with Vectors

Post by lABl »

Hi @Macciza,

I thought you wanted for a solution to get equal arrays size when "adding arrays of non equal size" :D, so I posted "my workaround" I didn't see brian's add script, I have checked it now and it makes basically the same but a bit in a different way, same behaviour truncating the array though.

Array vs vector in lemur I really think on them as you mentioned.

Cheers,
AB
m127
Newbie
Posts: 32
Joined: 14 Dec 2011 07:57

Re: Module for working with Vectors

Post by m127 »

well, yeah, I solved it as I foresaw. It seems that only a loop will do, like so:

Code: Select all

decl this, a, i, size, vectorA, vectorB;

vectorA={1,2,3,4,5};
vectorB={2,3,4};
a=0;
size=sizeof(vectorB);

for(i=0;i<size;i++)
{
if(vectorB[i]== vectorA){a[i]=1;}else{a[i]=0;}}

if(sumof(a)!=size){Monitor.value=0;}else{ Monitor.value=1;}
This above is a super-simplified example of what I am trying to accomplish (vector A is really like a 90 values vector).

It is a mere trick, but the fact is that I can't find a more efficient way to do the verification (whether or not vectorB's values are contained within vectorA).

For my project to work the way I want, I need to find a way to run about 70 verifications (ie, vector A is the "master" vector, but I have to verify another 69 vectors besides vectorB).

Could any of you help simplify the code above (make it more efficient/concise)?
Or do you think it's as concise as it will ever get?

As you can see, the verification takes places after "a" is filled with either 1's or 0's, then the sum of all of a's items is calculated and so if a's sum is equal to the size of vectorB, then we get a "true" or 1, else a "false" or 0.

The verification is rock solid now.

But I still wonder if there is a way to make a more efficient verification (less code).

Macciza wrote: m127 - Unfortunately I don't think the code deals with the array input . . .
Also do you want to work if the second array numbers are both in the first, or contiguously in the first - Do the 6 in a 100 need to be in order when they occur?

Cheers all
MM
No, order is irrelevant in my case scenario.

Macciza, what is the actual limitation in terms of characters or similar per script?

Just 6 verifications like the coded above started to turn some of the script text BLACK!!


This is how it looks without the breaks, but still, I have no idea where am I going to put the code to verify 70 vectors:

Code: Select all

decl this, a, i, size, vectorA, vectorB;
vectorA={1,2,3,4,5};vectorB={2,3,4};a=0;size=sizeof(vectorB);
for(i=0;i<size;i++){if(vectorB[i]== vectorA){a[i]=1;}else{a[i]=0;}}
if(sumof(a)!=size){Monitor.value=0;}else{ Monitor.value=1;}
Attachments
vectorVerificationByM127.jzml
by messing with the values of vectorB you realize how the verification actually works
(1.16 KiB) Downloaded 107 times
brianc
Regular
Posts: 87
Joined: 10 Jan 2012 02:16

Re: Module for working with Vectors

Post by brianc »

m127 wrote: What I need is some sort of function to determine whether vectorB's values are CONTAINED in vectorA, and then obtain a true or false result.
Obviously, the length/size of the vectors is oversimplified. As I said, I need to compare vectors with about 100 values vs smaller vectors with about 6 values.
Basically, this question is preventing me from finishing a project that I have been working for a while!!!
help!!!!
Whoa, sorry for the delay! The forum didn't tell me there were new posts in this thread for some reason. I'll try to combine a few posts:

I had originally intended contains() to look for a single value, so tthat's why it's not working for you as expected. I've updated the behavior of contains() in version 1.2 of SLVector (online now, including some basic documentation), so hopefully that's another way to get what you were looking at. You can now just do:

Code: Select all

SLVector.contains(a,b)
Which will return 1 if vector a contains all of the values in vector b, and 0 otherwise. So SLVector.contains({1,2,3,4,5,6,7},{1,3,4}) would return 1, while SLVector.contains({1,2,3,4,5,6,7},{1,3,9}) would return 0.

It looks like you came up with a good solution!
m127 wrote:PS: Btw, what's up with you Brian putting "creative commons licensing" in the file for these somewhat simple functions that are actually public knowledge and common programming techniques especially in other languages and environments?
Just curious. I find it a bit over the top.
Yeah, I definitely don't mean to be pretentious or to claim that I invented these basic algorithms. I just want to be explicit that people can use the module as a whole however they wish. For example, one of my templates was inspired a bit from another template, and since it wasn't clear what that template's author's terms were, I had to email him just to make sure it was ok. The CC license saves that step. It was also just me trying some ideas of how a community-based module ecosystem might work well.
Last edited by brianc on 09 Dec 2012 04:54, edited 2 times in total.
Post Reply