d: (1 2 3; 4 5 6)i: 1 1q).[d;i;+;2]1 2 34 7 6q).[d;i;%;2]'typeq).[d;i;%;“abc”]'typeWhy type error and how to solve it with amend? thanks in advance.
To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1257)
your d is an int matrix
if you divide by two the result is float
so either start with float matrix
q)d:(1 2 3f;4 5 6f)
q).[d;i;%;2]
1 2 3
4 2.5 6
or use integer division
q)d: (1 2 3; 4 5 6)
q).[d;i;div;2]
1 2 3
4 2 6
also note that characters cannot be used in arithmetic
(they can be used for indexing though)
Cheers,
Attila
On 17 Mar 2012, at 02:08, CL Jason wrote:
> d: (1 2 3; 4 5 6)
> i: 1 1
>
> q).[d;i;+;2]
> 1 2 3
> 4 7 6
>
> q).[d;i;%;2]
> 'type
> q).[d;i;%;“abc”]
> 'type
>
> Why type error and how to solve it with amend? thanks in advance.
>
> –
> You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
> To unsubscribe from this group, send email to =
personal-kdbplus+unsubscribe@googlegroups.com.
> For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.
>
The result from 5%2 is a fraction and hence, it gives the type error.
This can be verified as :?
q)d: (1.0 2.0 3.0; 4.0 5.0 6.0)
q).[d;i;%;2]
1 2 ? 3
4 2.5 6
If you want to do integer division you can use the div verb which returns the floor of X%Y.
q)d: (1 2 3; 4 5 6)
q).[d;i;div;2]
1 2 3
4 2 6
Cheers !
Sayantan.?
Sorry, late response leading to repetation.
Thanks for your answers. However,d is a general list:q)d: (1 2 3; 4 5 6)q)type d0hWhat’s more, in Q for Mortals, at each subdomain I, .[d;I;f;y] isequivalent to (d . I) f y, but:q)(d . 1 1) , “abc"5"a”“b”"c"q).[d;1 1;,;“abc”]'type
Hi CL,
I think they are two different things altogether.?
When you try,?(d . 1 1) , “abc” ?; you are appending “abc” to the element in d[1;1]. Hence, you get a general list as output. You are just selecting an item of d for this operation.
Excerpt from Q for Mortals :?
?For a list, the result is the item-wise application to the items of?L?indexed at depth?by?I, of?f?and the parameter?y.?
When you are doing,?.[d;1 1;,;“abc”]. You are trying to do the item-wise application on d which gives the error as d cannot keep a general list as it is not the type of d.?
Example :?
q).[d;1 1;+;5]
1 2 ?3
4 10 6
q)d
1 2 3
4 5 6
/d does not change
q).[`d;1 1;+;5]
`d
q)d
1 2 ?3
4 10 6
/d changes
q)d
1 2 ?3
4 10 6
q)(d . 1 1)+5
15
q)d
1 2 ?3
4 10 6
/d does not change
q)(`d . 1 1)+5
15
q)d
1 2 ?3
4 10 6
/here also d does not change as you select an element of d?
Cheers !
Sayantan.
Hi Sayantan
q)d: (1 2 3; 4 5 6)
q)type d
0h
And I don’t use `d to change d
CL,
Yes I get that. But when you use . then you apply the function on the elements of d and you could have stored it if you used d. However, in the other case you are just selecting an element of d and cannot store it even if you use
d.?
I hope I am able to clear your doubt. May be Atilla can chip in with a more crisp and concise explanation.
Cheers!
Sayantan.
I doubt if you also get that I’m not using `d and I only use d to
construct sth that derives from d but might have different types than
d. Thanks anyway.
To: personal-kdbplus@googlegroups.com
X-Mailer: Apple Mail (2.1257)
Sayantan is right
amend is type-safe even if you don’t reassign back with `d
and the problem is that although d would stay general list
but the second vector inside would change to general list
q)type each d
6 6h
what you would want to do
q)d[1]:d[1;0],(d[1;1]%2),d[1;2]
q)d
1 2 3
4 2.5 6
q)type each d
6 0h
note that the the previous version - k3 - worked as you expect:
d:(1 2 3;4 5 6)
.[d;1 1;%;2]
(1 2 3
(4;2.5;6))
this kind of type-safety was put in for our own good
as it is usually a bad idea performance-wise
or a logic in error trying to do something like that
Cheers,
Attila
On 17 Mar 2012, at 11:43, CL Jason wrote:
> I doubt if you also get that I’m not using d and I only use d to \> construct sth that derives from d but might have different types than \> d. Thanks anyway. \> \> On Sat, Mar 17, 2012 at 7:35 PM, Sayantan Ghosh \> <sayantan.ghosh5> wrote:<br>>> CL,<br>>> <br>>> Yes I get that. But when you use . then you apply the function on the<br>>> elements of d and you could have stored it if you used
d. However, =
in the
>> other case you are just selecting an element of d and cannot store it =
even
>> if you use d.<br>>> <br>>> I hope I am able to clear your doubt. May be Atilla can chip in with =<br>a more<br>>> crisp and concise explanation.<br>>> <br>>> Cheers!<br>>> Sayantan.<br>>> <br>>> <br>>> On Sat, Mar 17, 2012 at 4:50 PM, CL Jason <cl.jason> wrote:<br>>>> <br>>>> Hi Sayantan<br>>>> q)d: (1 2 3; 4 5 6)<br>>>> q)type d<br>>>> 0h<br>>>> And I don't use
d to change d
>>>
>>> On Sat, Mar 17, 2012 at 7:06 PM, Sayantan Ghosh
>>> <sayantan.ghosh5> wrote:
>>>> Hi CL,
>>>>
>>>> I think they are two different things altogether.
>>>>
>>>> When you try, (d . 1 1) , “abc” ; you are appending “abc” to the
>>>> element in
>>>> d[1;1]. Hence, you get a general list as output. You are just =
selecting
>>>> an
>>>> item of d for this operation.
>>>>
>>>> Excerpt from Q for Mortals :
>>>> For a list, the result is the item-wise application to the items
>>>> of L indexed at depth by I, of f and the parameter y.
>>>>
>>>> When you are doing, .[d;1 1;,;“abc”]. You are trying to do the =
item-wise
>>>> application on d which gives the error as d cannot keep a general =
list
>>>> as it
>>>> is not the type of d.
>>>>
>>>> Example :
>>>> q).[d;1 1;+;5]
>>>> 1 2 3
>>>> 4 10 6
>>>> q)d
>>>> 1 2 3
>>>> 4 5 6
>>>> /d does not change
>>>> q).[`d;1 1;+;5]
>>>> `d
>>>> q)d
>>>> 1 2 3
>>>> 4 10 6
>>>> /d changes
>>>>
>>>> q)d
>>>> 1 2 3
>>>> 4 10 6
>>>> q)(d . 1 1)+5
>>>> 15
>>>> q)d
>>>> 1 2 3
>>>> 4 10 6
>>>> /d does not change
>>>> q)(`d . 1 1)+5
>>>> 15
>>>> q)d
>>>> 1 2 3
>>>> 4 10 6
>>>> /here also d does not change as you select an element of d
>>>>
>>>> Cheers !
>>>> Sayantan.
>>>>
>>>> On Sat, Mar 17, 2012 at 4:06 PM, CL Jason <cl.jason> =
wrote:
>>>>>
>>>>> Thanks for your answers. However,d is a general list:
>>>>> q)d: (1 2 3; 4 5 6)
>>>>> q)type d
>>>>> 0h
>>>>> What’s more, in Q for Mortals, at each subdomain I, .[d;I;f;y] is
>>>>> equivalent to (d . I) f y, but:
>>>>> q)(d . 1 1) , “abc”
>>>>> 5
>>>>> “a”
>>>>> “b”
>>>>> “c”
>>>>> q).[d;1 1;,;“abc”]
>>>>> 'type
>>>>>
>>>>> –
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups
>>>>> “Kdb+ Personal Developers” group.
>>>>> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
>>>>> To unsubscribe from this group, send email to
>>>>> personal-kdbplus+unsubscribe@googlegroups.com.
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/personal-kdbplus?hl=en.
>>>>>
>>>>
>>>>
>>>>
>>>>
>>>> –
>>>> You received this message because you are subscribed to the Google
>>>> Groups
>>>> “Kdb+ Personal Developers” group.
>>>> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
>>>> To unsubscribe from this group, send email to
>>>> personal-kdbplus+unsubscribe@googlegroups.com.
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/personal-kdbplus?hl=en.
>>>
>>> –
>>> You received this message because you are subscribed to the Google =
Groups
>>> “Kdb+ Personal Developers” group.
>>> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> personal-kdbplus+unsubscribe@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/personal-kdbplus?hl=en.
>>>
>>
>>
>>
>> –
>> Regards,
>> Sayantan Ghosh, Associate,
>> Global Banking and Markets,
>> Royal Bank of Scotland.
>> Mobile : +91-82876-89163.
>>
>> –
>> You received this message because you are subscribed to the Google =
Groups
>> “Kdb+ Personal Developers” group.
>> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
>> To unsubscribe from this group, send email to
>> personal-kdbplus+unsubscribe@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/personal-kdbplus?hl=en.
>
> –
> You received this message because you are subscribed to the Google =
Groups “Kdb+ Personal Developers” group.
> To post to this group, send email to =
personal-kdbplus@googlegroups.com.
> To unsubscribe from this group, send email to =
personal-kdbplus+unsubscribe@googlegroups.com.
> For more options, visit this group at =
http://groups.google.com/group/personal-kdbplus?hl=en.
>
</cl.jason></sayantan.ghosh5></cl.jason></sayantan.ghosh5>