Return constant from plugin

Return constant from plugin - Return a constant value from inside a plugin - Messages

#1 Posted: 9/20/2021 1:35:14 PM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

Hello all,
I'm new to SMath Studio, and I'm developing a plugin in VB.NET. I need to define a constant given from my code (for example, I want to type in "const=" and get an hardcoded value).
I saw that TermType can be Operand, Operator, Function or Bracket. I defined my constant as a function, and in this way I always have to type const(1) to get the value (i.e. I must use a dummy argument, which is not needed).

Do you have any hint on that? Can I defined a constant in a plugin?

thanks in advance
#2 Posted: 9/20/2021 1:40:30 PM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

As I know it is not possible. All plugin stuff (if they are not regions) registered as functions. But I've never tried registering an operand instead of a function.

I tried. You can register the operand, but an error occurs when you try to calculate it numerically.
Russia ☭ forever, Viacheslav N. Mezentsev
#3 Posted: 9/20/2021 2:03:28 PM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

Thans for replying
It seems I cannot also use system paths as a function arguments; I suspect there's something about encoding: I have myfunc("C:\test.txt") and inside plugin code I get "C\003A\\test\002E\txt"
002E is "." in UTF8, but there's also escape chars.
#4 Posted: 9/20/2021 2:06:49 PM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

You must use TermsConverter.DecodeText() and TermsConverter.EncodeText() for this. Examples can be found in the repository.
Russia ☭ forever, Viacheslav N. Mezentsev
#5 Posted: 9/20/2021 3:50:43 PM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

Thanks a lot. Final question: can a function return a boolean?
#6 Posted: 9/20/2021 4:33:08 PM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

The program supports operations with numbers presented in various forms (natural, integer, rational, complex). There is no such type as boolean numbers. There are boolean operations defined on the above types. Therefore, there are numbers and operations on them, strings and operations on them, matrices and operations on them. The type of a variable is determined by its value. In symbolic form, you can return any expression allowed by the program syntax.

Therefore, if you need to return a boolean value from a plugin, then it needs to be converted to a value supported in API (see TNumber constructors). Usually, true - 1, false - 0.
Russia ☭ forever, Viacheslav N. Mezentsev
#7 Posted: 9/21/2021 11:25:19 AM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

Thanks, hence it would be easier to put result in an expression with a boolean operator and then return result.

I'm just wondering how to return vectors, I cannot understand from Core Documentation how to return a vector as a result. I suppose one can use some thing like: result = New Entry(New Numeric.TMatrix(... ? ))
and using 1 column only.
can you help on this?

thanks in advance

EDIT:
I did it like this - now the question is: is there a shorter way?

            Dim val() As Double = {1,2,3,4,5,6}
            Dim m(5, 0) As Numeric.TNumber
            For i = 0 To 5
                m(i, 0) = New Numeric.TNumber(val(i))
            Next
            Dim mout As New Numeric.TMatrix(m)
            result = Entry.Create(mout.ToTerms)
#8 Posted: 9/21/2021 3:06:30 PM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

You can find examples in repository. Try to search in *.cs files using keyword TMatrix. And then convert to the equivalent on vb.net.
Russia ☭ forever, Viacheslav N. Mezentsev
#9 Posted: 9/21/2021 3:49:52 PM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

I already saw that page, thanks. Don't know if it's for vb instead of c#, but I cannot have "result = solution" since this conversion is not permitted.

Now I'm looking how to associate units (eg. N, m or MPa, etc.) for result - I didn't find any code sample for this (I thought this https://smath.info:8443/!/#public/view/head/plugins/CustomFunctions/SMathFunctions/Units/UoM.cs was ok, but I cannot understand how units are associated to result). In my case, units are not inherited from function arguments.
#10 Posted: 9/21/2021 5:30:37 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

I cannot understand how units are associated to result


Associate unit compatible to functional operand
as exemplified ... integration is m² km² ...

Page12 Unit_Bolean.sm (13 KiB) downloaded 36 time(s).

#11 Posted: 9/21/2021 5:37:52 PM
overlord

overlord

552 likes in 1332 posts.

Group: Moderator

Wrote

Associate unit compatible to functional operand
as exemplified ... integration is m² km² ...

Page12 Unit_Bolean.sm (13 KiB) downloaded 36 time(s).


The questions are about source code of a plugin.
Not about how to write boolean expressions in SMath.
This post is not related with topic.
If you don't get what is asked, don't answer it.
#12 Posted: 9/21/2021 6:08:23 PM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

For example you can add units using TDouble. To determine the unit, you need to add an apostrophe in front of the text. For TryEvaluateExpression() you can use other methods (classes).

Public Class Plugin
    Implements IPluginMathNumericEvaluation

    Private _terms As TermInfo()
    
    Private _description As Dictionary(Of String, String) = New Dictionary(Of String, String) From {
        {"ENG", "test function."},
        {"RUS", "функция тестирования."}
    }

    Public Function GetTermsHandled(sessionProfile As SessionProfile) As TermInfo() Implements IPluginHandleEvaluation.GetTermsHandled        
        Return _terms
    End Function

    Public Sub Initialize() Implements IPlugin.Initialize
    
        _terms = {New TermInfo("test", TermType.[Function], _description("ENG", FunctionSections.Unknown, True) }
            
    End Sub

    Public Function NumericEvaluation(value As Term, args As TNumber(), ByRef result As TNumber) As Boolean Implements IPluginMathNumericEvaluation.NumericEvaluation
        
        result = args(0) * ( new TDouble("'sec" )
                
        Return True
        
    End Function

    Public Sub Dispose() Implements IDisposable.Dispose
    End Sub
End Class   
Russia ☭ forever, Viacheslav N. Mezentsev
1 users liked this post
johnnyontheweb 9/22/2021 3:57:00 AM
#13 Posted: 10/12/2021 4:38:19 AM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

Hello,
I looked in the code by I cannot find something similar to what I'm trying to do with my plugin.
A power of a length unit has to be returned as measure, but this is not working:
New TNumber(area * New TDouble("'m^2" ) )

What am I missing? It seems that "^" operand is considered in a different way when typed in SMath Studio and when coming from a plugin.
#14 Posted: 10/13/2021 7:26:17 AM
johnnyontheweb

johnnyontheweb

3 likes in 26 posts.

Group: Moderator

I found an alternative, please tell if there's something shorter than this:

New TNumber(area * DirectCast(New TDouble("'m" ).Pow(New TDouble(2) ), TDouble))
  • New Posts New Posts
  • No New Posts No New Posts