How to check type of variable in Fricas?

8 views
Skip to first unread message

Nasser M. Abbasi

unread,
Aug 31, 2022, 1:14:22 AM8/31/22
to FriCAS - computer algebra system
I am learning Fricas with the hope I can write Fricas script for CAS integration tests directly.

I am having hard time figuring how to check the type of variable. I looked at Fricas book but it shows no examples.

(11) -> n:Integer  
                                                                   Type: Void
(12) -> n:=10
   (12)  10
                                                                Type: Integer
(13) -> typeOf(n)
   (13)  Integer

Now I want to check, using if then else, that "n" has type "Integer". How to this? For example, this gives error

     if typeOf(n)=Integer then output("type is integer")

I tried few other permutations of the above, but they all give errors.

Thanks
--Nasser




Grégory Vanuxem

unread,
Aug 31, 2022, 2:27:09 AM8/31/22
to fricas...@googlegroups.com
Hello,

I suspect typeOf is an interpreter function that does not mix well with library things.

As a workaround may I suggest to coerce its output to OutputForm like this :
typeOf(n)::OutputForm = Integer => output("type is integer")

__
Greg


--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fricas-devel/8a0019d9-e2d0-44c9-a899-682e4361e6a4n%40googlegroups.com.

Grégory Vanuxem

unread,
Aug 31, 2022, 2:37:28 AM8/31/22
to fricas...@googlegroups.com
Le mer. 31 août 2022 à 08:26, Grégory Vanuxem <g.va...@gmail.com> a écrit :
Hello,

I suspect typeOf is an interpreter function that does not mix well with library things.

As a workaround may I suggest to coerce its output to OutputForm like this :
typeOf(n)::OutputForm = Integer => output("type is integer")

or eventually using Integer() instead of Integer above.

Nasser M. Abbasi

unread,
Aug 31, 2022, 2:49:24 AM8/31/22
to FriCAS - computer algebra system
Thanks. Both of these worked

(5) -> if typeOf(n)::OutputForm=Integer then output("yes")
   yes
                                                                   Type: Void
(6) -> if typeOf(n)::OutputForm=Integer() then output("yes")
   yes

What is the difference between Integer() and just Integer? I do not understand why Integer() even worked. What the empty () doing there?

--Nasser

Grégory Vanuxem

unread,
Aug 31, 2022, 3:26:02 AM8/31/22
to fricas...@googlegroups.com
Maybe you'll have more information here but from what I know Integer is a shortcut for the Integer() constructor since Integer is a Domain without parameters. But other domains need parameters.

Waldek Hebisch

unread,
Aug 31, 2022, 12:23:04 PM8/31/22
to 'Nasser M. Abbasi' via FriCAS - computer algebra system
On Tue, Aug 30, 2022 at 10:14:21PM -0700, 'Nasser M. Abbasi' via FriCAS - computer algebra system wrote:
> I am learning Fricas with the hope I can write Fricas script for CAS
> integration tests directly.
>
> I am having hard time figuring how to check the type of variable. I looked
> at Fricas book but it shows no examples.
>
> (11) -> n:Integer
> Type:
> Void
> (12) -> n:=10
> (12) 10
> Type:
> Integer
> (13) -> typeOf(n)
> (13) Integer
>
> Now I want to check, using if then else, that "n" has type "Integer". How
> to this? For example, this gives error

But why do you want to do this? When you declare that variable
is of type Integer, then it has this type, no need to check.

I could say more about 'typeOf', but there is good chance that
what you really want to do is something else. Intstead I give
you an example:

(13) -> 1::Expression(Integer)

(13) 1
Type: Expression(Integer)
(14) -> typeOf(1::Expression(Integer))

(14) Expression(Integer)
Type: Type
(15) -> retract(1::Expression(Integer))@Integer

(15) 1
Type: Integer

Note 1: In simple uses 'typeOf' is redundant becase interpreter already
prints types.

Note 2: Something that logically is an integer may be embedded in
larger type. 'retractIfCan' allows you to check if expression has
integral value. If you are sure that value is integral, then
'retract' is simpler.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages