Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Functions that return arrays

I am trying to a UDF that uses arrays and worksheet matrix functions to
calculate the intersection of three spheres. I have declared several 1- and
2-D double arrays to store data.

I originally declared these array variables as doubles, but I found I could
not use functions like the following example:

Dim tempMatrix(1 to 3, 1 to 3) as Double
tempVector = identity(tempVector)

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix. I imagine that I cannot assign the function's return to
tempVector because it is a double array, not a Variant (i.e. not an object).

I declared these variables as Doubles because I have read that variants are
slow and inefficient and should be avoided if possible. How then would be
the best way to fill these variable? Should I use subroutines and send the
double array as a parameter? Or should I use variant arrays?

But then, variant arrays cannot be initialized with dimensions, right? I.e:

Dim tempVector(1 to 3, 1 to 3) as Variant

I'm looking for the Best Practice here. Thanks.
Pflugs
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Functions that return arrays

Use variants since that is what is required to do what you want.

Dim tempVector as Variant
ReDim tempVector(1 to 3, 1 to 3)

--
Regards,
Tom Ogilvy



"Pflugs" wrote:

I am trying to a UDF that uses arrays and worksheet matrix functions to
calculate the intersection of three spheres. I have declared several 1- and
2-D double arrays to store data.

I originally declared these array variables as doubles, but I found I could
not use functions like the following example:

Dim tempMatrix(1 to 3, 1 to 3) as Double
tempVector = identity(tempVector)

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix. I imagine that I cannot assign the function's return to
tempVector because it is a double array, not a Variant (i.e. not an object).

I declared these variables as Doubles because I have read that variants are
slow and inefficient and should be avoided if possible. How then would be
the best way to fill these variable? Should I use subroutines and send the
double array as a parameter? Or should I use variant arrays?

But then, variant arrays cannot be initialized with dimensions, right? I.e:

Dim tempVector(1 to 3, 1 to 3) as Variant

I'm looking for the Best Practice here. Thanks.
Pflugs

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 167
Default Functions that return arrays

How do I use the matrix functions with varaint arrays? Do I first have to
create a similar double array?

"Tom Ogilvy" wrote:

Use variants since that is what is required to do what you want.

Dim tempVector as Variant
ReDim tempVector(1 to 3, 1 to 3)

--
Regards,
Tom Ogilvy



"Pflugs" wrote:

I am trying to a UDF that uses arrays and worksheet matrix functions to
calculate the intersection of three spheres. I have declared several 1- and
2-D double arrays to store data.

I originally declared these array variables as doubles, but I found I could
not use functions like the following example:

Dim tempMatrix(1 to 3, 1 to 3) as Double
tempVector = identity(tempVector)

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix. I imagine that I cannot assign the function's return to
tempVector because it is a double array, not a Variant (i.e. not an object).

I declared these variables as Doubles because I have read that variants are
slow and inefficient and should be avoided if possible. How then would be
the best way to fill these variable? Should I use subroutines and send the
double array as a parameter? Or should I use variant arrays?

But then, variant arrays cannot be initialized with dimensions, right? I.e:

Dim tempVector(1 to 3, 1 to 3) as Variant

I'm looking for the Best Practice here. Thanks.
Pflugs

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Functions that return arrays

If you have to use a double, then adjust your identity function so it isn't a
function but a subroutine.

Arrays are passed by reference, so if you work on it in the identity
function, you don't have to "return" it. Just call your Identity sub with
the tempVector as an argument

sub Main()
Identity TempVector
' now just use the altered tempVector

end sub

Public sub Identity( t() as Double )
' code that alters "t"
' no code like Identity = t
End sub

--
Regards,
Tom Ogilvy


"Pflugs" wrote:

How do I use the matrix functions with varaint arrays? Do I first have to
create a similar double array?

"Tom Ogilvy" wrote:

Use variants since that is what is required to do what you want.

Dim tempVector as Variant
ReDim tempVector(1 to 3, 1 to 3)

--
Regards,
Tom Ogilvy



"Pflugs" wrote:

I am trying to a UDF that uses arrays and worksheet matrix functions to
calculate the intersection of three spheres. I have declared several 1- and
2-D double arrays to store data.

I originally declared these array variables as doubles, but I found I could
not use functions like the following example:

Dim tempMatrix(1 to 3, 1 to 3) as Double
tempVector = identity(tempVector)

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix. I imagine that I cannot assign the function's return to
tempVector because it is a double array, not a Variant (i.e. not an object).

I declared these variables as Doubles because I have read that variants are
slow and inefficient and should be avoided if possible. How then would be
the best way to fill these variable? Should I use subroutines and send the
double array as a parameter? Or should I use variant arrays?

But then, variant arrays cannot be initialized with dimensions, right? I.e:

Dim tempVector(1 to 3, 1 to 3) as Variant

I'm looking for the Best Practice here. Thanks.
Pflugs

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Functions that return arrays

I don't vouch for "best practice", but if the functions in the freely
downloadable file at http://home.pacbell.net/beban are available to your
workbook, you might consider

Assign identity(tempVector), tempVector

Alan Beban

Pflugs wrote:
. . .
I originally declared these array variables as doubles, but I found I could
not use functions like the following example:

Dim tempMatrix(1 to 3, 1 to 3) as Double
tempVector = identity(tempVector)

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix. I imagine that I cannot assign the function's return to
tempVector because it is a double array, not a Variant (i.e. not an object). . . .



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default Functions that return arrays

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix.


Instead of using two loops, here's something I might use:

Sub MainCode()
Dim m As Variant
m = IdentityMatrix(3)
End Sub

Function IdentityMatrix(n)
IdentityMatrix = Evaluate(Replace("--(ROW(#)=COLUMN(#))", "#",
[A1].Resize(n, n).Address))
End Function

--
Dana DeLouis


"Pflugs" wrote in message
...
I am trying to a UDF that uses arrays and worksheet matrix functions to
calculate the intersection of three spheres. I have declared several 1-
and
2-D double arrays to store data.

I originally declared these array variables as doubles, but I found I
could
not use functions like the following example:

Dim tempMatrix(1 to 3, 1 to 3) as Double
tempVector = identity(tempVector)

where "identity" is a UDF that accepts a square array and sets it to an
identity matrix. I imagine that I cannot assign the function's return to
tempVector because it is a double array, not a Variant (i.e. not an
object).

I declared these variables as Doubles because I have read that variants
are
slow and inefficient and should be avoided if possible. How then would be
the best way to fill these variable? Should I use subroutines and send
the
double array as a parameter? Or should I use variant arrays?

But then, variant arrays cannot be initialized with dimensions, right?
I.e:

Dim tempVector(1 to 3, 1 to 3) as Variant

I'm looking for the Best Practice here. Thanks.
Pflugs



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions for manipulating arrays Alan Beban[_2_] Excel Discussion (Misc queries) 0 September 24th 07 07:05 AM
Functions/arrays LLama Excel Discussion (Misc queries) 3 July 11th 07 10:23 AM
Arrays parameters in Functions BruceK Excel Worksheet Functions 4 June 30th 05 11:40 AM
Advice for VBA functions using arrays Don Taylor Excel Programming 9 December 24th 04 09:11 PM
Arrays as Arguments in Functions Dean[_6_] Excel Programming 3 June 24th 04 12:28 PM


All times are GMT +1. The time now is 05:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"