PC Review


Reply
Thread Tools Rate Thread

Array Declaration Problem ??

 
 
monir
Guest
Posts: n/a
 
      21st Jun 2008
(This is a cross-post)

Hello;

I'm trying to correctly pass the range B11:C14 to the Function MyRoots() and
return the results by the array function to cells I11:J13.

Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
MyRoots() would produce a compile error: "Constant expresion required", and
also I couldn't declare "a" as a 2D dynamic array!

I suspect the array variables declaration in the following example is the
problem.
The array Function MyRoots() incorrectly returns 0.0 results to cells I11:J13.

cell B8::3
cell B9: myTrue
cells B11:C14 numerical values
cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}

Function MyRoots (a, m As Integer, polish As String)
ReDim a(m + 1, 2) As Double
ReDim roots(m, 2) As Double
Dim j As Integer, its As Integer
Dim x(2) As Double
ReDim ad(m + 1, 2) As Double
.......................................my code............
For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
.......................................my code............
Call Laguer (ad, j, x, its)
.......................................my code............
roots(j, 1) = x(1)
roots(j, 2) = x(2)
.......................................my code............
MyRoots = roots
End Function

Sub Laguer (a, m, x, its)
Dim x1(2) As Double
.......................................my code............
x(1) = x1(1)
x(2) = x1(2)
.......................................my code............
Exit Sub
End Sub

Your expert help would be greatly appreciated.

Regards.
 
Reply With Quote
 
 
 
 
Mark Ivey
Guest
Posts: n/a
 
      21st Jun 2008
Lately I have been using a lot of GLOBAL declarations to avoid some
complications in passing my variables...

I typically would declare my variable (or array) like this at the top of the
module I need to use it in:

Public myArray(1 To 1000, 1 To 1000) As String

Also I have been declaring my arrays to an upper bound number that should
accommodate any scenario (ie - 10,000 or 100,000 depending on the
situation). You should also be able to use a ReDim elsewhere in your code,
but I do not used that feature myself.

Then I can reference it anywhere in my module (a sub routine or a function)
and never have to pass a variable.

I know this is not the most efficient method, but it works out pretty good
for me...

Just a thought...

NOTE: I do not use this method for all my variable (arrays)... Just the ones
I need to reference in other sub-routines or functions.

Mark Ivey



"monir" <(E-Mail Removed)> wrote in message
news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
> (This is a cross-post)
>
> Hello;
>
> I'm trying to correctly pass the range B11:C14 to the Function MyRoots()
> and
> return the results by the array function to cells I11:J13.
>
> Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
> MyRoots() would produce a compile error: "Constant expresion required",
> and
> also I couldn't declare "a" as a 2D dynamic array!
>
> I suspect the array variables declaration in the following example is the
> problem.
> The array Function MyRoots() incorrectly returns 0.0 results to cells
> I11:J13.
>
> cell B8::3
> cell B9: myTrue
> cells B11:C14 numerical values
> cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
>
> Function MyRoots (a, m As Integer, polish As String)
> ReDim a(m + 1, 2) As Double
> ReDim roots(m, 2) As Double
> Dim j As Integer, its As Integer
> Dim x(2) As Double
> ReDim ad(m + 1, 2) As Double
> ......................................my code............
> For j = 1 To m + 1
> ad(j, 1) = a(j, 1)
> ad(j, 2) = a(j, 2)
> Next j
> ......................................my code............
> Call Laguer (ad, j, x, its)
> ......................................my code............
> roots(j, 1) = x(1)
> roots(j, 2) = x(2)
> ......................................my code............
> MyRoots = roots
> End Function
>
> Sub Laguer (a, m, x, its)
> Dim x1(2) As Double
> ......................................my code............
> x(1) = x1(1)
> x(2) = x1(2)
> ......................................my code............
> Exit Sub
> End Sub
>
> Your expert help would be greatly appreciated.
>
> Regards.


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      21st Jun 2008
I am not clear what your code is doing, especially the ,,, my code ... bits,
but your array declaration is definitely wrong.

Try this

Function MyRoots(a, m As Integer, polish As String)
Dim a As Variant, Roots As Variant
Dim ad As Variant
Dim j As Integer, its As Integer
Dim x(2) As Double

ReDim a(m + 1, 2)
ReDim Roots(m, 2)
ReDim ad(m + 1, 2)
For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
Call Laguer(ad, j, x, its)
Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
MyRoots = Roots
End Function


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"monir" <(E-Mail Removed)> wrote in message
news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
> (This is a cross-post)
>
> Hello;
>
> I'm trying to correctly pass the range B11:C14 to the Function MyRoots()
> and
> return the results by the array function to cells I11:J13.
>
> Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
> MyRoots() would produce a compile error: "Constant expresion required",
> and
> also I couldn't declare "a" as a 2D dynamic array!
>
> I suspect the array variables declaration in the following example is the
> problem.
> The array Function MyRoots() incorrectly returns 0.0 results to cells
> I11:J13.
>
> cell B8::3
> cell B9: myTrue
> cells B11:C14 numerical values
> cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
>
> Function MyRoots (a, m As Integer, polish As String)
> ReDim a(m + 1, 2) As Double
> ReDim roots(m, 2) As Double
> Dim j As Integer, its As Integer
> Dim x(2) As Double
> ReDim ad(m + 1, 2) As Double
> ......................................my code............
> For j = 1 To m + 1
> ad(j, 1) = a(j, 1)
> ad(j, 2) = a(j, 2)
> Next j
> ......................................my code............
> Call Laguer (ad, j, x, its)
> ......................................my code............
> roots(j, 1) = x(1)
> roots(j, 2) = x(2)
> ......................................my code............
> MyRoots = roots
> End Function
>
> Sub Laguer (a, m, x, its)
> Dim x1(2) As Double
> ......................................my code............
> x(1) = x1(1)
> x(2) = x1(2)
> ......................................my code............
> Exit Sub
> End Sub
>
> Your expert help would be greatly appreciated.
>
> Regards.



 
Reply With Quote
 
monir
Guest
Posts: n/a
 
      21st Jun 2008
Hi Mark;

Thank you for your thoughts.
I think the declaration statement ReDim a(m+1,2) in my sample code Function
MyRoots() should be removed altogether, since the array is passing to the
function as an array argument ! Now I'm getting #VALUE! results instead of 0.
Using global declarations and sizing arrays to the max expected are possible
workarounds, but don't you agree that having the array declarations
automatically adjusted to the situation "looks" more efficient ??

Regards.


"Mark Ivey" wrote:

> Lately I have been using a lot of GLOBAL declarations to avoid some
> complications in passing my variables...
>
> I typically would declare my variable (or array) like this at the top of the
> module I need to use it in:
>
> Public myArray(1 To 1000, 1 To 1000) As String
>
> Also I have been declaring my arrays to an upper bound number that should
> accommodate any scenario (ie - 10,000 or 100,000 depending on the
> situation). You should also be able to use a ReDim elsewhere in your code,
> but I do not used that feature myself.
>
> Then I can reference it anywhere in my module (a sub routine or a function)
> and never have to pass a variable.
>
> I know this is not the most efficient method, but it works out pretty good
> for me...
>
> Just a thought...
>
> NOTE: I do not use this method for all my variable (arrays)... Just the ones
> I need to reference in other sub-routines or functions.
>
> Mark Ivey
>
>
>
> "monir" <(E-Mail Removed)> wrote in message
> news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
> > (This is a cross-post)
> >
> > Hello;
> >
> > I'm trying to correctly pass the range B11:C14 to the Function MyRoots()
> > and
> > return the results by the array function to cells I11:J13.
> >
> > Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
> > MyRoots() would produce a compile error: "Constant expresion required",
> > and
> > also I couldn't declare "a" as a 2D dynamic array!
> >
> > I suspect the array variables declaration in the following example is the
> > problem.
> > The array Function MyRoots() incorrectly returns 0.0 results to cells
> > I11:J13.
> >
> > cell B8::3
> > cell B9: myTrue
> > cells B11:C14 numerical values
> > cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
> >
> > Function MyRoots (a, m As Integer, polish As String)
> > ReDim a(m + 1, 2) As Double
> > ReDim roots(m, 2) As Double
> > Dim j As Integer, its As Integer
> > Dim x(2) As Double
> > ReDim ad(m + 1, 2) As Double
> > ......................................my code............
> > For j = 1 To m + 1
> > ad(j, 1) = a(j, 1)
> > ad(j, 2) = a(j, 2)
> > Next j
> > ......................................my code............
> > Call Laguer (ad, j, x, its)
> > ......................................my code............
> > roots(j, 1) = x(1)
> > roots(j, 2) = x(2)
> > ......................................my code............
> > MyRoots = roots
> > End Function
> >
> > Sub Laguer (a, m, x, its)
> > Dim x1(2) As Double
> > ......................................my code............
> > x(1) = x1(1)
> > x(2) = x1(2)
> > ......................................my code............
> > Exit Sub
> > End Sub
> >
> > Your expert help would be greatly appreciated.
> >
> > Regards.

>

 
Reply With Quote
 
monir
Guest
Posts: n/a
 
      21st Jun 2008
Hi Bob;

Thank you kindly for your prompt reply.
I've tried your suggestion.

Function MyRoots(a, m As Integer, polish As String)
Dim a As Variant, Roots As Variant
Dim ad As Variant
Dim j As Integer, its As Integer
Dim x(2) As Double

ReDim a(m + 1, 2)
ReDim Roots(m, 2)
ReDim ad(m + 1, 2)
> ..............................my code............

For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
> ..............................my code............

Call Laguer (ad, j, x, its)
> ..............................my code............

Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
MyRoots = Roots
End Function

It produces the compile error: "Duplicate declaration in current scope" with
reference to the "a" declaration in:
Dim a As Variant, Roots As Variant

Any suggestion ??

Regards.

"Bob Phillips" wrote:

> I am not clear what your code is doing, especially the ,,, my code ... bits,
> but your array declaration is definitely wrong.
>
> Try this
>
> Function MyRoots(a, m As Integer, polish As String)
> Dim a As Variant, Roots As Variant
> Dim ad As Variant
> Dim j As Integer, its As Integer
> Dim x(2) As Double
>
> ReDim a(m + 1, 2)
> ReDim Roots(m, 2)
> ReDim ad(m + 1, 2)
> For j = 1 To m + 1
> ad(j, 1) = a(j, 1)
> ad(j, 2) = a(j, 2)
> Next j
> Call Laguer(ad, j, x, its)
> Roots(j, 1) = x(1)
> Roots(j, 2) = x(2)
> MyRoots = Roots
> End Function
>
>
> --
> HTH
>
> Bob
>
> (there's no email, no snail mail, but somewhere should be gmail in my addy)
>
> "monir" <(E-Mail Removed)> wrote in message
> news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
> > (This is a cross-post)
> >
> > Hello;
> >
> > I'm trying to correctly pass the range B11:C14 to the Function MyRoots()
> > and
> > return the results by the array function to cells I11:J13.
> >
> > Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
> > MyRoots() would produce a compile error: "Constant expresion required",
> > and
> > also I couldn't declare "a" as a 2D dynamic array!
> >
> > I suspect the array variables declaration in the following example is the
> > problem.
> > The array Function MyRoots() incorrectly returns 0.0 results to cells
> > I11:J13.
> >
> > cell B8::3
> > cell B9: myTrue
> > cells B11:C14 numerical values
> > cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
> >
> > Function MyRoots (a, m As Integer, polish As String)
> > ReDim a(m + 1, 2) As Double
> > ReDim roots(m, 2) As Double
> > Dim j As Integer, its As Integer
> > Dim x(2) As Double
> > ReDim ad(m + 1, 2) As Double
> > ......................................my code............
> > For j = 1 To m + 1
> > ad(j, 1) = a(j, 1)
> > ad(j, 2) = a(j, 2)
> > Next j
> > ......................................my code............
> > Call Laguer (ad, j, x, its)
> > ......................................my code............
> > roots(j, 1) = x(1)
> > roots(j, 2) = x(2)
> > ......................................my code............
> > MyRoots = roots
> > End Function
> >
> > Sub Laguer (a, m, x, its)
> > Dim x1(2) As Double
> > ......................................my code............
> > x(1) = x1(1)
> > x(2) = x1(2)
> > ......................................my code............
> > Exit Sub
> > End Sub
> >
> > Your expert help would be greatly appreciated.
> >
> > Regards.

>
>
>

 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      21st Jun 2008
Sorry, missed the parameter

Function MyRoots(a As Variant, m As Integer, polish As String)
Dim Roots As Variant
Dim ad As Variant
Dim j As Integer, its As Integer
Dim x(2) As Double

ReDim a(m + 1, 2)
ReDim Roots(m, 2)
ReDim ad(m + 1, 2)
'> ..............................my code............
For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
'> ..............................my code............
Call Laguer(ad, j, x, its)
'> ..............................my code............
Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
MyRoots = Roots
End Function



--
__________________________________
HTH

Bob

"monir" <(E-Mail Removed)> wrote in message
news:F36BF0A8-CAFE-4D7F-9FE6-(E-Mail Removed)...
> Hi Bob;
>
> Thank you kindly for your prompt reply.
> I've tried your suggestion.
>
> Function MyRoots(a, m As Integer, polish As String)
> Dim a As Variant, Roots As Variant
> Dim ad As Variant
> Dim j As Integer, its As Integer
> Dim x(2) As Double
>
> ReDim a(m + 1, 2)
> ReDim Roots(m, 2)
> ReDim ad(m + 1, 2)
>> ..............................my code............

> For j = 1 To m + 1
> ad(j, 1) = a(j, 1)
> ad(j, 2) = a(j, 2)
> Next j
>> ..............................my code............

> Call Laguer (ad, j, x, its)
>> ..............................my code............

> Roots(j, 1) = x(1)
> Roots(j, 2) = x(2)
> MyRoots = Roots
> End Function
>
> It produces the compile error: "Duplicate declaration in current scope"
> with
> reference to the "a" declaration in:
> Dim a As Variant, Roots As Variant
>
> Any suggestion ??
>
> Regards.
>
> "Bob Phillips" wrote:
>
>> I am not clear what your code is doing, especially the ,,, my code ...
>> bits,
>> but your array declaration is definitely wrong.
>>
>> Try this
>>
>> Function MyRoots(a, m As Integer, polish As String)
>> Dim a As Variant, Roots As Variant
>> Dim ad As Variant
>> Dim j As Integer, its As Integer
>> Dim x(2) As Double
>>
>> ReDim a(m + 1, 2)
>> ReDim Roots(m, 2)
>> ReDim ad(m + 1, 2)
>> For j = 1 To m + 1
>> ad(j, 1) = a(j, 1)
>> ad(j, 2) = a(j, 2)
>> Next j
>> Call Laguer(ad, j, x, its)
>> Roots(j, 1) = x(1)
>> Roots(j, 2) = x(2)
>> MyRoots = Roots
>> End Function
>>
>>
>> --
>> HTH
>>
>> Bob
>>
>> (there's no email, no snail mail, but somewhere should be gmail in my
>> addy)
>>
>> "monir" <(E-Mail Removed)> wrote in message
>> news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
>> > (This is a cross-post)
>> >
>> > Hello;
>> >
>> > I'm trying to correctly pass the range B11:C14 to the Function
>> > MyRoots()
>> > and
>> > return the results by the array function to cells I11:J13.
>> >
>> > Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
>> > MyRoots() would produce a compile error: "Constant expresion required",
>> > and
>> > also I couldn't declare "a" as a 2D dynamic array!
>> >
>> > I suspect the array variables declaration in the following example is
>> > the
>> > problem.
>> > The array Function MyRoots() incorrectly returns 0.0 results to cells
>> > I11:J13.
>> >
>> > cell B8::3
>> > cell B9: myTrue
>> > cells B11:C14 numerical values
>> > cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
>> >
>> > Function MyRoots (a, m As Integer, polish As String)
>> > ReDim a(m + 1, 2) As Double
>> > ReDim roots(m, 2) As Double
>> > Dim j As Integer, its As Integer
>> > Dim x(2) As Double
>> > ReDim ad(m + 1, 2) As Double
>> > ......................................my code............
>> > For j = 1 To m + 1
>> > ad(j, 1) = a(j, 1)
>> > ad(j, 2) = a(j, 2)
>> > Next j
>> > ......................................my code............
>> > Call Laguer (ad, j, x, its)
>> > ......................................my code............
>> > roots(j, 1) = x(1)
>> > roots(j, 2) = x(2)
>> > ......................................my code............
>> > MyRoots = roots
>> > End Function
>> >
>> > Sub Laguer (a, m, x, its)
>> > Dim x1(2) As Double
>> > ......................................my code............
>> > x(1) = x1(1)
>> > x(2) = x1(2)
>> > ......................................my code............
>> > Exit Sub
>> > End Sub
>> >
>> > Your expert help would be greatly appreciated.
>> >
>> > Regards.

>>
>>
>>



 
Reply With Quote
 
monir
Guest
Posts: n/a
 
      21st Jun 2008
Hi Bob;

Thanks again. I've done the correction:

Function MyRoots (a As Variant, m As Integer, polish As String)
Dim Roots As Variant
Dim ad As Variant
Dim j As Integer, its As Integer
Dim x(2) As Double

ReDim a(m + 1, 2)
ReDim Roots(m, 2)
ReDim ad(m + 1, 2)
' ..............................my code............
For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
' ..............................my code............
Call Laguer (ad, j, x, its)
' ..............................my code............
Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
MyRoots = Roots
End Function

The array function MyRoots() returns all zeros (same as was posted
originally).
It seems to me that by declaring the incoming array "a" using:
ReDim a(m+1,2)
it sets all elements of matrix "a" to 0.0. The Immediate Window for
MyRoots() confirms that. I even tried: ReDim Preserve a(m+1,2) with no
change.
(If you prefer, I would be glad to post or send you the function MyRoots()
and its sub Laguer() code. Total ~ 40 lines.)

Regards.

"Bob Phillips" wrote:

> Sorry, missed the parameter
>
> Function MyRoots(a As Variant, m As Integer, polish As String)
> Dim Roots As Variant
> Dim ad As Variant
> Dim j As Integer, its As Integer
> Dim x(2) As Double
>
> ReDim a(m + 1, 2)
> ReDim Roots(m, 2)
> ReDim ad(m + 1, 2)
> '> ..............................my code............
> For j = 1 To m + 1
> ad(j, 1) = a(j, 1)
> ad(j, 2) = a(j, 2)
> Next j
> '> ..............................my code............
> Call Laguer(ad, j, x, its)
> '> ..............................my code............
> Roots(j, 1) = x(1)
> Roots(j, 2) = x(2)
> MyRoots = Roots
> End Function
>
>
>
> --
> __________________________________
> HTH
>
> Bob
>
> "monir" <(E-Mail Removed)> wrote in message
> news:F36BF0A8-CAFE-4D7F-9FE6-(E-Mail Removed)...
> > Hi Bob;
> >
> > Thank you kindly for your prompt reply.
> > I've tried your suggestion.
> >
> > Function MyRoots(a, m As Integer, polish As String)
> > Dim a As Variant, Roots As Variant
> > Dim ad As Variant
> > Dim j As Integer, its As Integer
> > Dim x(2) As Double
> >
> > ReDim a(m + 1, 2)
> > ReDim Roots(m, 2)
> > ReDim ad(m + 1, 2)
> >> ..............................my code............

> > For j = 1 To m + 1
> > ad(j, 1) = a(j, 1)
> > ad(j, 2) = a(j, 2)
> > Next j
> >> ..............................my code............

> > Call Laguer (ad, j, x, its)
> >> ..............................my code............

> > Roots(j, 1) = x(1)
> > Roots(j, 2) = x(2)
> > MyRoots = Roots
> > End Function
> >
> > It produces the compile error: "Duplicate declaration in current scope"
> > with
> > reference to the "a" declaration in:
> > Dim a As Variant, Roots As Variant
> >
> > Any suggestion ??
> >
> > Regards.
> >
> > "Bob Phillips" wrote:
> >
> >> I am not clear what your code is doing, especially the ,,, my code ...
> >> bits,
> >> but your array declaration is definitely wrong.
> >>
> >> Try this
> >>
> >> Function MyRoots(a, m As Integer, polish As String)
> >> Dim a As Variant, Roots As Variant
> >> Dim ad As Variant
> >> Dim j As Integer, its As Integer
> >> Dim x(2) As Double
> >>
> >> ReDim a(m + 1, 2)
> >> ReDim Roots(m, 2)
> >> ReDim ad(m + 1, 2)
> >> For j = 1 To m + 1
> >> ad(j, 1) = a(j, 1)
> >> ad(j, 2) = a(j, 2)
> >> Next j
> >> Call Laguer(ad, j, x, its)
> >> Roots(j, 1) = x(1)
> >> Roots(j, 2) = x(2)
> >> MyRoots = Roots
> >> End Function
> >>
> >>
> >> --
> >> HTH
> >>
> >> Bob
> >>
> >> (there's no email, no snail mail, but somewhere should be gmail in my
> >> addy)
> >>
> >> "monir" <(E-Mail Removed)> wrote in message
> >> news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
> >> > (This is a cross-post)
> >> >
> >> > Hello;
> >> >
> >> > I'm trying to correctly pass the range B11:C14 to the Function
> >> > MyRoots()
> >> > and
> >> > return the results by the array function to cells I11:J13.
> >> >
> >> > Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
> >> > MyRoots() would produce a compile error: "Constant expresion required",
> >> > and
> >> > also I couldn't declare "a" as a 2D dynamic array!
> >> >
> >> > I suspect the array variables declaration in the following example is
> >> > the
> >> > problem.
> >> > The array Function MyRoots() incorrectly returns 0.0 results to cells
> >> > I11:J13.
> >> >
> >> > cell B8::3
> >> > cell B9: myTrue
> >> > cells B11:C14 numerical values
> >> > cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
> >> >
> >> > Function MyRoots (a, m As Integer, polish As String)
> >> > ReDim a(m + 1, 2) As Double
> >> > ReDim roots(m, 2) As Double
> >> > Dim j As Integer, its As Integer
> >> > Dim x(2) As Double
> >> > ReDim ad(m + 1, 2) As Double
> >> > ......................................my code............
> >> > For j = 1 To m + 1
> >> > ad(j, 1) = a(j, 1)
> >> > ad(j, 2) = a(j, 2)
> >> > Next j
> >> > ......................................my code............
> >> > Call Laguer (ad, j, x, its)
> >> > ......................................my code............
> >> > roots(j, 1) = x(1)
> >> > roots(j, 2) = x(2)
> >> > ......................................my code............
> >> > MyRoots = roots
> >> > End Function
> >> >
> >> > Sub Laguer (a, m, x, its)
> >> > Dim x1(2) As Double
> >> > ......................................my code............
> >> > x(1) = x1(1)
> >> > x(2) = x1(2)
> >> > ......................................my code............
> >> > Exit Sub
> >> > End Sub
> >> >
> >> > Your expert help would be greatly appreciated.
> >> >
> >> > Regards.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
monir
Guest
Posts: n/a
 
      23rd Jun 2008
Hello;

1) It appears that the issue is a bit more difficult than I initially thought.
The latest version of the procedure is provided below. It returns #VALUE! to
cells I11:J13, which in all probabilities is the result of incorrect
declarations of the arrays.
Notice that I commented out the declaration ReDim a(m + 1, 2) in Function
MyRoots(), otherwise it would re-set all elements of the incoming matrix "a"
to 0.0 and the array function would return 0.0s to cells I11:J13.

2) Here's the latest version:
cell B8::3
cell B9: myTrue
cells B11:C14 numerical values
cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}

Function MyRoots (a As Variant, m As Integer, polish As String)
Dim Roots As Variant
Dim ad As Variant
Dim j As Integer, its As Integer
Dim x(2) As Double
' ReDim a(m + 1, 2)
ReDim Roots(m, 2)
ReDim ad(m + 1, 2)
' ..............................my code............
For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j
' ..............................my code............
Call Laguer (ad, j, x, its)
' ..............................my code............
Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
MyRoots = Roots
End Function

Sub Laguer (a, m, x, its)
Dim x1(2) As Double
' .................................my code............
x(1) = x1(1)
x(2) = x1(2)
'.................................my code............
Exit Sub
End Sub

3) The following article deals specifically with the issue at hand:
http://www.cpearson.com/Excel/Passin...ningArrays.htm
The difficulty in the article is two folds. First: the article deals with
passing and returning multi-dimensional arrays but it is written for the
experts in VBA, and second: the described procedure deals with Subroutines
calling Functions, and not the other way around!
The general theme in the article, however, appears to be that dynamic arrays
MUST be used. But what if the function is calling the subroutine ??

4) If it helps, I would be glad to post or email the entire Function
MyRoots() and Sub Laguer() (total ~ 40 lines), together with the w/s input
values and the expected returned results.

Thank you kindly.

"monir" wrote:

> Hi Bob;
>
> Thanks again. I've done the correction:
>
> Function MyRoots (a As Variant, m As Integer, polish As String)
> Dim Roots As Variant
> Dim ad As Variant
> Dim j As Integer, its As Integer
> Dim x(2) As Double
>
> ReDim a(m + 1, 2)
> ReDim Roots(m, 2)
> ReDim ad(m + 1, 2)
> ' ..............................my code............
> For j = 1 To m + 1
> ad(j, 1) = a(j, 1)
> ad(j, 2) = a(j, 2)
> Next j
> ' ..............................my code............
> Call Laguer (ad, j, x, its)
> ' ..............................my code............
> Roots(j, 1) = x(1)
> Roots(j, 2) = x(2)
> MyRoots = Roots
> End Function
>
> The array function MyRoots() returns all zeros (same as was posted
> originally).
> It seems to me that by declaring the incoming array "a" using:
> ReDim a(m+1,2)
> it sets all elements of matrix "a" to 0.0. The Immediate Window for
> MyRoots() confirms that. I even tried: ReDim Preserve a(m+1,2) with no
> change.
> (If you prefer, I would be glad to post or send you the function MyRoots()
> and its sub Laguer() code. Total ~ 40 lines.)
>
> Regards.
>
> "Bob Phillips" wrote:
>
> > Sorry, missed the parameter
> >
> > Function MyRoots(a As Variant, m As Integer, polish As String)
> > Dim Roots As Variant
> > Dim ad As Variant
> > Dim j As Integer, its As Integer
> > Dim x(2) As Double
> >
> > ReDim a(m + 1, 2)
> > ReDim Roots(m, 2)
> > ReDim ad(m + 1, 2)
> > '> ..............................my code............
> > For j = 1 To m + 1
> > ad(j, 1) = a(j, 1)
> > ad(j, 2) = a(j, 2)
> > Next j
> > '> ..............................my code............
> > Call Laguer(ad, j, x, its)
> > '> ..............................my code............
> > Roots(j, 1) = x(1)
> > Roots(j, 2) = x(2)
> > MyRoots = Roots
> > End Function
> >
> >
> >
> > --
> > __________________________________
> > HTH
> >
> > Bob
> >
> > "monir" <(E-Mail Removed)> wrote in message
> > news:F36BF0A8-CAFE-4D7F-9FE6-(E-Mail Removed)...
> > > Hi Bob;
> > >
> > > Thank you kindly for your prompt reply.
> > > I've tried your suggestion.
> > >
> > > Function MyRoots(a, m As Integer, polish As String)
> > > Dim a As Variant, Roots As Variant
> > > Dim ad As Variant
> > > Dim j As Integer, its As Integer
> > > Dim x(2) As Double
> > >
> > > ReDim a(m + 1, 2)
> > > ReDim Roots(m, 2)
> > > ReDim ad(m + 1, 2)
> > >> ..............................my code............
> > > For j = 1 To m + 1
> > > ad(j, 1) = a(j, 1)
> > > ad(j, 2) = a(j, 2)
> > > Next j
> > >> ..............................my code............
> > > Call Laguer (ad, j, x, its)
> > >> ..............................my code............
> > > Roots(j, 1) = x(1)
> > > Roots(j, 2) = x(2)
> > > MyRoots = Roots
> > > End Function
> > >
> > > It produces the compile error: "Duplicate declaration in current scope"
> > > with
> > > reference to the "a" declaration in:
> > > Dim a As Variant, Roots As Variant
> > >
> > > Any suggestion ??
> > >
> > > Regards.
> > >
> > > "Bob Phillips" wrote:
> > >
> > >> I am not clear what your code is doing, especially the ,,, my code ...
> > >> bits,
> > >> but your array declaration is definitely wrong.
> > >>
> > >> Try this
> > >>
> > >> Function MyRoots(a, m As Integer, polish As String)
> > >> Dim a As Variant, Roots As Variant
> > >> Dim ad As Variant
> > >> Dim j As Integer, its As Integer
> > >> Dim x(2) As Double
> > >>
> > >> ReDim a(m + 1, 2)
> > >> ReDim Roots(m, 2)
> > >> ReDim ad(m + 1, 2)
> > >> For j = 1 To m + 1
> > >> ad(j, 1) = a(j, 1)
> > >> ad(j, 2) = a(j, 2)
> > >> Next j
> > >> Call Laguer(ad, j, x, its)
> > >> Roots(j, 1) = x(1)
> > >> Roots(j, 2) = x(2)
> > >> MyRoots = Roots
> > >> End Function
> > >>
> > >>
> > >> --
> > >> HTH
> > >>
> > >> Bob
> > >>
> > >> (there's no email, no snail mail, but somewhere should be gmail in my
> > >> addy)
> > >>
> > >> "monir" <(E-Mail Removed)> wrote in message
> > >> news:8636FAA1-9C87-4924-AFE6-(E-Mail Removed)...
> > >> > (This is a cross-post)
> > >> >
> > >> > Hello;
> > >> >
> > >> > I'm trying to correctly pass the range B11:C14 to the Function
> > >> > MyRoots()
> > >> > and
> > >> > return the results by the array function to cells I11:J13.
> > >> >
> > >> > Notice the use of ReDim. Declaring Dim a(m+1,2) As Double in Function
> > >> > MyRoots() would produce a compile error: "Constant expresion required",
> > >> > and
> > >> > also I couldn't declare "a" as a 2D dynamic array!
> > >> >
> > >> > I suspect the array variables declaration in the following example is
> > >> > the
> > >> > problem.
> > >> > The array Function MyRoots() incorrectly returns 0.0 results to cells
> > >> > I11:J13.
> > >> >
> > >> > cell B8::3
> > >> > cell B9: myTrue
> > >> > cells B11:C14 numerical values
> > >> > cells I11:J13:: array function {=MyRoots(B11:C14, B8, B9)}
> > >> >
> > >> > Function MyRoots (a, m As Integer, polish As String)
> > >> > ReDim a(m + 1, 2) As Double
> > >> > ReDim roots(m, 2) As Double
> > >> > Dim j As Integer, its As Integer
> > >> > Dim x(2) As Double
> > >> > ReDim ad(m + 1, 2) As Double
> > >> > ......................................my code............
> > >> > For j = 1 To m + 1
> > >> > ad(j, 1) = a(j, 1)
> > >> > ad(j, 2) = a(j, 2)
> > >> > Next j
> > >> > ......................................my code............
> > >> > Call Laguer (ad, j, x, its)
> > >> > ......................................my code............
> > >> > roots(j, 1) = x(1)
> > >> > roots(j, 2) = x(2)
> > >> > ......................................my code............
> > >> > MyRoots = roots
> > >> > End Function
> > >> >
> > >> > Sub Laguer (a, m, x, its)
> > >> > Dim x1(2) As Double
> > >> > ......................................my code............
> > >> > x(1) = x1(1)
> > >> > x(2) = x1(2)
> > >> > ......................................my code............
> > >> > Exit Sub
> > >> > End Sub
> > >> >
> > >> > Your expert help would be greatly appreciated.
> > >> >
> > >> > Regards.
> > >>
> > >>
> > >>

> >
> >
> >

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      23rd Jun 2008
> 4) If it helps, I would be glad to post or email the entire Function
> MyRoots() and Sub Laguer() (total ~ 40 lines), together with the
> w/s input values and the expected returned results.


I don't know about anyone else, but doing this would be helpful for me in
determining if I can help you out or not. And make sure you copy/paste your
function and subroutine's code (do not simply retype them); also check to
make sure you post the correct expected values for the example input values
you give to us.

Rick

 
Reply With Quote
 
monir
Guest
Posts: n/a
 
      23rd Jun 2008
Hello;

In summary, I'm trying to correctly pass the range B11:C14 to Function
Zroots() and return the results by the array function to cells I11:J13.

Here's the latest attempt which returns #VALUE! error values to cells
I11:J13, most likely because of the incorrect array declarations in the
procedure. I'm reasonably confident about the math since I've the same
procedure working perfectly in Fortran.

Work Sheet values:
cell B8::3
cell B9:: myTrue
cells I11:J13:: array function {=Zroots(B11:C14, B8, B9)}

cell B11::-80525640.7629787
cell C11:: 0.0
cell B12::-78862.110289658
cell C12:: 0.0
cell B13::660.968663533082
cell C13:: 0.0
cell B14::1.0
cell C14:: 0.0

Expected results:
cell I11::-494.480795275
cell J11:: 31.6075878057
cell I12::-494.480795275
cell J12::-31.6075878057
cell I13:: 327.992927018
cell J13:: 0.0

Procedure:
...................................................................
Option Base 1
Option Explicit
Function Zroots(a As Variant, m As Integer, polish As String) As Double
Dim Roots As Variant
Dim ad As Variant
Dim j As Integer, its As Integer
Dim x(2) As Double
' ReDim a(m + 1, 2)
ReDim Roots(m, 2)
ReDim ad(m + 1, 2)

Dim EPS As Double
Dim i As Integer, jj As Integer
Dim b(2) As Double, c(2) As Double

EPS = 0.000001

For j = 1 To m + 1
ad(j, 1) = a(j, 1)
ad(j, 2) = a(j, 2)
Next j

For j = m To 1 Step -1
x(1) = 0#
x(2) = 0#
Call Laguer(ad, j, x, its)
Debug.Print "step 001, Fun Zroots " & its & " Iterations, " & Time
Debug.Print " Fun Zroots, real a(" & j & ",1)= " & a(j, 1)
Debug.Print " Fun Zroots, imag a(" & j & ",2)= " & a(j, 2)
If Abs(x(2)) <= 2# * EPS ^ 2 * Abs(x(1)) Then x(1) = x(1): x(2) = 0#
Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
b(1) = ad(j + 1, 1)
b(2) = ad(j + 1, 2)
For jj = j To 1 Step -1
c(1) = ad(jj, 1)
c(2) = ad(jj, 2)
ad(jj, 1) = b(1)
ad(jj, 2) = b(2)
b(1) = x(1) * b(1) - x(2) * b(2) + c(1)
b(2) = x(1) * b(2) + x(2) * b(1) + c(2)
Next jj
Next j

If polish = "myTrue" Then
For j = 1 To m
x(1) = Roots(j, 1)
x(2) = Roots(j, 2)
Call Laguer(a, m, x, its)
Roots(j, 1) = x(1)
Roots(j, 2) = x(2)
Next j
End If

For j = 2 To m
x(1) = Roots(j, 1)
x(2) = Roots(j, 2)
For i = j - 1 To 1 Step -1
If Roots(i, 1) <= x(1) Then GoTo MyLine1
Roots(i + 1, 1) = Roots(i, 1)
Roots(i + 1, 2) = Roots(i, 2)
Next i
i = 0
MyLine1:
Roots(i + 1, 1) = x(1)
Roots(i + 1, 2) = x(2)
Next j
Zroots = Roots
End Function
...................................................................
Sub Laguer(a, m, x, its)
Dim MAXIT As Integer, MT As Integer
Dim EPSS As Double
Dim iter As Integer, j As Integer
Dim abx As Double, abp As Double, abm As Double, myErr As Double
Dim frac(8) As Double
Dim dx(2) As Double, x1(2) As Double, b(2) As Double, d(2) As Double, f(2)
As Double, g(2) As Double
Dim h(2) As Double, sq(2) As Double, gp(2) As Double, gm(2) As Double,
g2(2) As Double
Dim dxx As Double, b12 As Double
Dim xsq As Double, ysq As Double, r As Double, theta As Double

EPSS = 0.0000002

MT = 10
MAXIT = MT * 8
frac(1) = 0.5: frac(2) = 0.25: frac(3) = 0.75: frac(4) = 0.13: frac(5) = 0.38
frac(6) = 0.62: frac(7) = 0.88: frac(8) = 1#

For iter = 1 To MAXIT
its = iter
b(1) = a(m + 1, 1)
b(2) = a(m + 1, 2)
myErr = (b(1) ^ 2 + b(2) ^ 2) ^ (1 / 2)
d(1) = 0#
d(2) = 0#
f(1) = 0#
f(2) = 0#
abx = (x(1) ^ 2 + x(2) ^ 2) ^ (1 / 2)
For j = m To 1 Step -1
f(1) = x(1) * f(1) - x(2) * f(2) + d(1)
f(2) = x(1) * f(2) + x(2) * f(1) + d(2)
d(1) = x(1) * d(1) - x(2) * d(2) + b(1)
d(2) = x(1) * d(2) + x(2) * d(1) + b(2)
b(1) = x(1) * b(1) - x(2) * b(2) + a(j, 1)
b(2) = x(1) * b(2) + x(2) * b(1) + a(j, 2)
myErr = (b(1) ^ 2 + b(2) ^ 2) ^ (1 / 2) + abx * myErr
Next j
myErr = EPSS * myErr
If (b(1) ^ 2 + b(2) ^ 2) ^ (1 / 2) <= myErr Then
Debug.Print "step 002, Sub Laguer, a root: a+bi = " & x(1) & "+" &
x(2) & "i"
Exit Sub
Else
b12 = (b(1) ^ 2 + b(2) ^ 2)
g(1) = (d(1) * b(1) + d(2) * b(2)) / b12
g(2) = (d(2) * b(1) - d(1) * b(2)) / b12
g2(1) = g(1) ^ 2 - g(2) ^ 2
g2(2) = 2 * g(1) * g(2)
h(1) = g2(1) - 2 * (f(1) * b(1) + f(2) * b(2)) / b12
h(2) = g2(2) - 2 * (f(2) * b(1) - f(1) * b(2)) / b12
xsq = (m - 1) * (m * h(1) - g2(1))
ysq = (m - 1) * (m * h(2) - g2(2))
r = (xsq ^ 2 + ysq ^ 2) ^ (1 / 2)
theta = WorksheetFunction.Atan2(xsq, ysq)
sq(1) = r ^ (1 / 2) * Cos(theta / 2)
sq(2) = r ^ (1 / 2) * Sin(theta / 2)
gp(1) = g(1) + sq(1)
gp(2) = g(2) + sq(2)
gm(1) = g(1) - sq(1)
gm(2) = g(2) - sq(2)
abp = (gp(1) ^ 2 + gp(2) ^ 2) ^ (1 / 2)
abm = (gm(1) ^ 2 + gm(2) ^ 2) ^ (1 / 2)
If abp < abm Then gp(1) = gm(1): gp(2) = gm(2)
If WorksheetFunction.Max(abp, abm) > 0# Then
dx(1) = m * gp(1) / (gp(1) ^ 2 + gp(2) ^ 2)
dx(2) = -m * gp(2) / (gp(1) ^ 2 + gp(2) ^ 2)
Else
dxx = WorksheetFunction.Cosh(WorksheetFunction.Ln(1# + abx)) + _
WorksheetFunction.Sinh(WorksheetFunction.Ln(1# + abx))
dx(1) = dxx * Cos(CDbl(iter))
dx(2) = dxx * Sin(CDbl(iter))
End If
End If
x1(1) = x(1) - dx(1)
x1(2) = x(2) - dx(2)
If x(1) = x1(1) And x(2) = x1(2) Then Exit Sub
If iter - (Int(iter / MT) * MT) <> 0 Then
x(1) = x1(1)
x(2) = x1(2)
Else
x(1) = x(1) - dx(1) * frac(Int(iter / MT))
x(2) = x(2) - dx(2) * frac(Int(iter / MT))
End If
Next iter
MsgBox "Too many iterations in Sub Laguer. Very unusual!"
Exit Sub
End Sub

Thank you kindly, will very much appreciate your help.

"Rick Rothstein (MVP - VB)" wrote:

> > 4) If it helps, I would be glad to post or email the entire Function
> > MyRoots() and Sub Laguer() (total ~ 40 lines), together with the
> > w/s input values and the expected returned results.

>
> I don't know about anyone else, but doing this would be helpful for me in
> determining if I can help you out or not. And make sure you copy/paste your
> function and subroutine's code (do not simply retype them); also check to
> make sure you post the correct expected values for the example input values
> you give to us.
>
> Rick
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaration of array pramod Microsoft Excel Programming 0 18th Feb 2009 11:06 AM
Problem when I move an array declaration from within a method, to start of class? garyusenet@myway.com Microsoft C# .NET 3 6th Dec 2006 11:42 PM
Array Declaration mikey10 Microsoft Excel Programming 2 18th Oct 2004 08:19 PM
Array Declaration Syntax Jerry Camel Microsoft VB .NET 4 4th Mar 2004 10:18 PM
Array Declaration WFB Microsoft VB .NET 1 30th Jan 2004 03:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:32 PM.