possible error in extraction of elements

Bugs

Modérateur : xcasadmin

compsystems
Messages : 561
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

possible error in extraction of elements

Message par compsystems » ven. févr. 16, 2018 12:19 pm

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"

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: possible error in extraction of elements

Message par parisse » ven. févr. 16, 2018 4:50 pm

And where is the bug?

compsystems
Messages : 561
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Re: possible error in extraction of elements

Message par compsystems » ven. févr. 16, 2018 9:32 pm

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"

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: possible error in extraction of elements

Message par parisse » sam. févr. 17, 2018 6:41 am

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.

compsystems
Messages : 561
Inscription : sam. févr. 04, 2017 11:34 pm
Localisation : Colombia
Contact :

Re: possible error in extraction of elements

Message par compsystems » sam. févr. 17, 2018 2:59 pm

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?
Yes,
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

parisse
Messages : 5739
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: possible error in extraction of elements

Message par parisse » sam. févr. 17, 2018 3:31 pm

Looking at the source code, I won't change part(d,1) because the case is explictly coded (prog.cc, gen _part(...))

Code : Tout sélectionner

...
	if (g.type!=_SYMB){
	  if (i.val!=1)
	    return gensizeerr(contextptr);
	  return g;
	}
...
You should check the type of the object before calling part.

Répondre