How to get _overloadSize to return N_Dimensions=0? [message #88661] |
Tue, 27 May 2014 15:40  |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
I was wondering how it is possible to get the _overloadSize function to return N_DIMENSION=0 when there are no dimensions. Below is an example. The docs seems like it reports N_Elements(Size(input, /DIMENSIONS), which is not correct for an undefined variable...
IDL> print, size(!Null, /N_DIMENSIONS)
0
IDL> .run test_olsize__define
Dimensions: [0]
N_Elements: 0
N_Dimensions: 1
;-----------------------------------------------
function test_olSize::_OverloadSize
return, size(*self.value, /N_DIMENSIONS)
end
function test_olSize::Init
compile_opt strictarr
self.value = Ptr_New(/ALLOCATE_HEAP)
return, 1
end
pro test_olSize__define
class = {test_olSize, $
inherits IDL_Object, $
value: ptr_new()}
end
;Main level test program
myObj = Obj_New('Test_olSize')
print, 'Dimensions: ', '[' + strjoin(strtrim(size(myObj, /DIMENSIONS), 2), ', ') + ']'
print, 'N_Elements: ', strtrim(size(myObj, /N_ELEMENTS), 2)
print, 'N_Dimensions: ', strtrim(size(myObj, /N_DIMENSIONS), 2)
obj_destroy, myObj
end
|
|
|
|
Re: How to get _overloadSize to return N_Dimensions=0? [message #94355 is a reply to message #88662] |
Tue, 18 April 2017 06:56   |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
The problem with undefined variables was fixed, but there is another bug related to scalar values. I assume N_Elements is determined as Product(Size(input, /DIMENSIONS)). However, a scalar value has a dimension size of 0, so the result of 0 elements.
function test_olSize::_OverloadSize
return, size(*self.value, /DIMENSIONS)
end
function test_olSize::Init, value
compile_opt strictarr
self.value = Ptr_New(value, /NO_COPY)
return, 1
end
pro test_olSize__define
class = {test_olSize, $
inherits IDL_Object, $
value: ptr_new()}
end
IDL> myObj = Obj_New('Test_olSize', 1)
IDL> Print, N_Elements(myObj)
0
|
|
|
Re: How to get _overloadSize to return N_Dimensions=0? [message #94360 is a reply to message #94355] |
Wed, 19 April 2017 14:19   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 4/18/17 7:56 AM, Matthew Argall wrote:
> The problem with undefined variables was fixed, but there is another
> bug related to scalar values. I assume N_Elements is determined as
> Product(Size(input, /DIMENSIONS)). However, a scalar value has a
> dimension size of 0, so the result of 0 elements.
>
>
> function test_olSize::_OverloadSize
> return, size(*self.value, /DIMENSIONS)
> end
>
> function test_olSize::Init, value
> compile_opt strictarr
> self.value = Ptr_New(value, /NO_COPY)
> return, 1
> end
>
> pro test_olSize__define
> class = {test_olSize, $
> inherits IDL_Object, $
> value: ptr_new()}
> end
>
>
> IDL> myObj = Obj_New('Test_olSize', 1)
> IDL> Print, N_Elements(myObj)
> 0
>
I would use something like the following for your _overloadSize method:
function test_olsize::_overloadSize
return, size(*self.value, /n_dimensions) eq 0 $
? 1L $
: size(*self.value, /dimensions)
end
IDL> myObj = Obj_New('Test_olSize', 1)
IDL> Print, N_Elements(myObj)
1
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
|
|
|
Re: How to get _overloadSize to return N_Dimensions=0? [message #94368 is a reply to message #94360] |
Fri, 21 April 2017 09:48  |
Matthew Argall
Messages: 286 Registered: October 2011
|
Senior Member |
|
|
The fix has to be a bit more complicated. Using your suggestion,
IDL> myobj = Test_olSize(!Null)
IDL> Print, N_Elements(myobj)
1
function test_olSize::_OverloadSize
return, n_elements(*self.value) eq 0 ? 0L : $
size(*self.value, /n_dimensions) eq 0 ? 1L : $
size(*self.value, /dimensions)
end
IDL> myobj = Test_olSize(!Null)
IDL> Print, N_Elements(myobj)
0
Consequently, this fixes the original problem as well.
|
|
|