Examples
// EXTRACTION WITH []
[a,[b,c],d] [1] => [b,c] // ok
[a,[b,c],d] [2] => d // ok
([a,[b,c],d] [2])[1] => d[1] // "invalid dimension: 1, d has no elements"
[a,[b,c],d] [1,1] => c // ok
[a,[b,c],d] [1,1,1] => c[1] // "invalid dimension: 1, c has no elements"
[a,[b,c],d] [2,1] => d[1] // "invalid dimension: 1, d has no elements"
[a,[b,c],d] [2,1,1] => d[1,1] // "invalid dimension: 1, d has no elements"
[a,[b,c],d] [3] => // ok "Index outside range: 3, [a,[b,c],d] vector size is 3, last position 2"
([a,[b,c],3+d^2] [2])[1] => 3 // ok
([a,[b,c],3+d^2] [2])[2] => d^2 // ok
([a,[b,c],3+d^2] [2])[0] => '+' // ok
// EXTRACTION WITH ()
[a,[b,c],d] (1) => a // ok
[a,[b,c],d] (2) => [b,c] // ok
([a,[b,c],d] (2))(1) => b // ok
[a,[b,c],d] (1,1) => a(1) // "invalid dimension: 1, a has no elements"
[a,[b,c],d] (2,1) => b // ok
[a,[b,c],d] (2,1,1) => b(1) // "invalid dimension: 1, b has no elements"
[a,[b,c],d] (3) => d // ok
[a,[b,c],d] (2,2) => c
([a,[b,c],d] (2)) (2) => c
[a,[b,c],d] (4) // ok "Index outside range: 4, [a,[b,c],d] vector size is 3, last position 3"
possible error in extraction of elements
Modérateur : xcasadmin
-
- Messages : 561
- Inscription : sam. févr. 04, 2017 11:34 pm
- Localisation : Colombia
- Contact :
Re: possible error in extraction of elements
And where is the bug?
-
- Messages : 561
- Inscription : sam. févr. 04, 2017 11:34 pm
- Localisation : Colombia
- Contact :
Re: possible error in extraction of elements
Hello, the problem is in the postfix function (expression1)[n part] & part function part(expression1,n part)
A: Extraction with postfix function
(expression1)[n part]
([a,[b,c],d] [2])[1] => This should not return d[1]
([a,[b,c],d] [2])[1] => "invalid dimension: 1, d has no elements or parts"
B: Extraction with part function
part(d) returns 0 // ok 0 parts
part(d,0) returns "d" // ok
part(d,1) returns "d" bug // "invalid dimension: 1, d has no elements or parts"
As part(d) informs me that has zero parts, it does not make sense to extract the first part
part(expression1,n part) // part cmd
or
(expression1)[n part] // postfix cmd of part cmd
case1:
part(expression1) ⇒ number
Simplifies expression1 and returns the number of
top-level arguments or operands. This returns 0 if
expression1 is a number, variable, or symbolic
constant such as pi, e, i,
Examples:
part(cos(pi*x+3)) returns 1
part(d) returns 0
part(pi) returns 0
Note: cos(pi x+3) has one argument.
case2:
part(expression1, 0) ⇒ string
Simplifies expression1 and returns a string that
contains the top-level function name or operator.
This returns string(expression1) if expression1 is a
number, variable, or symbolic constant such as pi,
e, i, etc
Examples:
part(cos(pi*x+3),0) returns "cos"
part(d,0) returns "d"
part(pi,0) returns "pi"
cos(pi*x+3)[0] returns 'cos'
case3:
part(expression1, n) ⇒ expression
Simplifies expression1 and returns the nth argument
or operand, where n is > 0 and <= the number of
top-level arguments or operands returned by
part(expression1). Otherwise, an error is returned.
Examples:
part(cos(pi*x+3),1) returns pi*x+3
cos(pi*x+3)[1] returns pi*x+3
part(d,1) returns "error"
A: Extraction with postfix function
(expression1)[n part]
([a,[b,c],d] [2])[1] => This should not return d[1]
([a,[b,c],d] [2])[1] => "invalid dimension: 1, d has no elements or parts"
B: Extraction with part function
part(d) returns 0 // ok 0 parts
part(d,0) returns "d" // ok
part(d,1) returns "d" bug // "invalid dimension: 1, d has no elements or parts"
As part(d) informs me that has zero parts, it does not make sense to extract the first part
part(expression1,n part) // part cmd
or
(expression1)[n part] // postfix cmd of part cmd
case1:
part(expression1) ⇒ number
Simplifies expression1 and returns the number of
top-level arguments or operands. This returns 0 if
expression1 is a number, variable, or symbolic
constant such as pi, e, i,
Examples:
part(cos(pi*x+3)) returns 1
part(d) returns 0
part(pi) returns 0
Note: cos(pi x+3) has one argument.
case2:
part(expression1, 0) ⇒ string
Simplifies expression1 and returns a string that
contains the top-level function name or operator.
This returns string(expression1) if expression1 is a
number, variable, or symbolic constant such as pi,
e, i, etc
Examples:
part(cos(pi*x+3),0) returns "cos"
part(d,0) returns "d"
part(pi,0) returns "pi"
cos(pi*x+3)[0] returns 'cos'
case3:
part(expression1, n) ⇒ expression
Simplifies expression1 and returns the nth argument
or operand, where n is > 0 and <= the number of
top-level arguments or operands returned by
part(expression1). Otherwise, an error is returned.
Examples:
part(cos(pi*x+3),1) returns pi*x+3
cos(pi*x+3)[1] returns pi*x+3
part(d,1) returns "error"
Re: possible error in extraction of elements
If I asked you "where is the bug?" it's because your first post was too long and not clear. Unfortunately, your answer is too long and still not clear. Reading the beginning, I understand that you woud like part(d,1) to return an error if d is not an expression, is that all?
d[1] will remain so because it's the way you can work with indexed identifiers.
d[1] will remain so because it's the way you can work with indexed identifiers.
-
- Messages : 561
- Inscription : sam. févr. 04, 2017 11:34 pm
- Localisation : Colombia
- Contact :
Re: possible error in extraction of elements
Yes,parisse a écrit :I understand that you woud like part(d,1) to return an error if d is not an expression, is that all?
and part( expression1, -1 ) equal to part( expression1 )
with the purpose of that (expr)[-1] return the number of operands.
(3+d^4)[-1] => 2
part(3+d^4) => 2
part(3+d^4,-1) => 2
Re: possible error in extraction of elements
Looking at the source code, I won't change part(d,1) because the case is explictly coded (prog.cc, gen _part(...))
You should check the type of the object before calling part.
Code : Tout sélectionner
...
if (g.type!=_SYMB){
if (i.val!=1)
return gensizeerr(contextptr);
return g;
}
...