# Symbolic arithmetic operation • ### Question

• Hey guys. I'm trying to do a math operation symbolically, for example: I want to solve the equation (x-1)*(x-2) in the way that the answer would be (x^2-3*x+2). is there such a thing to do that? if yes. how should I define my "x" variable? (double,integer, ...).

Abed_4321

Wednesday, July 8, 2015 5:12 PM

• As a matter of fact, representing "x" in VB is fairly simple, but  what I will to do is to calculate the coefficient of a pretty difficult function. I could get the final answer for a given "x" (x= a number), but as I'm trying to build the function for a set of numbers, I need to calculate the coefficient of the X's ( X^(a_i) ) independently or just simply do the math operation with a symbolic "x". For example, to see what  (x-1)*(x-2) is gonna be, you can calculate the coefficient of x^2,x and the y-intercept or if you could calculate (x-2)*(x-1) directly.

Regards

Abed_4321

Well I think we can show you how to do it if you show can show us a complete example of what you want to do.

"what I will to do is to calculate the coefficient of a pretty difficult function"

What function? Show us and show the answer and describe the method you want to use. Show why the answer I gave you does not work or is not what you want.

'this program works properly

Public Class Form1
Public myarray As New List(Of Double)
Public myarray2 As New List(Of Double)
Dim n As Integer
Dim x As Double
Dim primaryk As Integer
Dim m As Integer

Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = False
TextBox4.Enabled = False

Button1.BackColor = Color.LightBlue
Button2.BackColor = Color.LightBlue
Button3.BackColor = Color.LightBlue
Button4.BackColor = Color.LightBlue

TextBox1.BackColor = Color.LightPink
TextBox2.BackColor = Color.LightPink
TextBox3.BackColor = Color.LightPink
TextBox4.BackColor = Color.LightPink
TextBox5.BackColor = Color.LightPink

ListBox1.BackColor = Color.Azure

Me.BackColor = Color.DarkOrchid

End Sub

'button2 gets the inputs of our lists and the number of pints
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

m = 0

Static k As Integer = 0
k = primaryk

On Error GoTo error_handler

If n = 0 Then
n = TextBox1.Text
End If

n = n - m

If k < n Then

On Error GoTo error_handler2

TextBox2.Clear()

On Error GoTo error_handler3

TextBox5.Clear()

End If

replace()

primaryk += 1
k = primaryk

If k >= n Then

Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = True
Button4.Enabled = True

Else
Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True

End If
m = n
Exit Sub

error_handler:
MessageBox.Show("Enter valid integer for 'n'")

TextBox1.Clear()
Exit Sub
error_handler2:
TextBox5.Clear()
TextBox2.Clear()

MessageBox.Show("Enter valid number for 'x_i'")

Exit Sub
error_handler3:
TextBox2.Clear()
TextBox5.Clear()
MessageBox.Show("Enter valid number for 'y_i'")
Exit Sub

End Sub

'Total() function does the main math operation
Private Function total() As Double

Dim i, j As Integer
Dim mlt As Double
Dim sum As Double

For i = 0 To myarray.Count - 1

mlt = 1

For j = 0 To myarray.Count - 1

If j <> i Then

'I want to do something like the following without asigning a specificing number to "x"

mlt = mlt * ((x - myarray.Item(j)) / (myarray.Item(i) - myarray.Item(j)))

End If

Next

sum += mlt * myarray2.Item(i)
Next
Return sum
End Function

'button1 get the "x" and does the final calculation stuff
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error GoTo Error_handler4

x = TextBox3.Text

Dim num As Integer

For num = 0 To myarray.Count - 1

Next

TextBox4.Text = total()
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Exit Sub

Error_handler4:
MessageBox.Show("Enter a valid number for x")
Exit Sub
End Sub

'button3 reset all the inputs

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
myarray.Clear()
myarray2.Clear()
n = 0
m = 0
x = 0
primaryk = 0

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
ListBox1.Items.Clear()

End Sub

'button4 will enable us of entering new number of points
Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()

Dim mymsg As Integer
mymsg = MsgBox("Do you want to use the EX-list?", 3, "Set Points Configuration")
If mymsg = 6 Then

MessageBox.Show("Enter the new value for n")

n = TextBox1.Text
n = n - m
m = n
ElseIf mymsg = 7 Then

Button3.PerformClick()

Else
MessageBox.Show("you suck")
End If
End Sub

'replace() function avoids duplication and caan replace the list's objects
Private Function replace() As Integer

Static index_i As Integer = 0
Dim dup, index_j As Integer

If index_i <> 0 Then

For index_j = 0 To myarray.Count - 2

If myarray.Item(index_i) = myarray.Item(index_j) Then
dup = MsgBox("Duplication is not allowed, Do you want to replace it with the last one?", 4, "Duplication Error")

If dup = 6 Then
myarray.Item(index_j) = myarray.Item(index_i)
myarray2.Item(index_j) = myarray2.Item(index_i)

myarray.RemoveAt(index_i)
myarray2.RemoveAt(index_i)

index_i = index_i - 1

n += 1
Exit For

Else

myarray.RemoveAt(index_i)
myarray2.RemoveAt(index_i)
index_i = index_i - 1
n += 1
Exit For
End If

End If

Next

End If

index_i += 1
Return n

End Function

End Class

Abed_4321

• Marked as answer by Friday, July 17, 2015 3:00 AM
Thursday, July 9, 2015 2:19 PM

### All replies

• Hi,

maybe http://symbolics.mathdotnet.com/

Regards,

Thorsten

Wednesday, July 8, 2015 5:29 PM
• Does it work for VB.NET? There is a symbolic way of doing arithmetic in Matlab ( syms command). I'm looking for that in VB.NET

Abed_4321

Wednesday, July 8, 2015 5:41 PM
• Hi, Why shouldnt it work with VB.Net? Just read the first line of text under the title in the document the link above points to.

From the free programs, Maxima also can do symbolic solutions, but I dont know if there's an api to use it from other programs.

Regards,

Thorsten

Wednesday, July 8, 2015 6:14 PM
• thanks, but it didn't work.

Abed_4321

Thursday, July 9, 2015 1:51 AM
• I went to an online Symbolic Differentiation Calculator and this CodeProject Link Symbolic Differentiation where I downloaded the source and demo project.

As can be seen in the image below both returned different answers for your equation (x-1)*(x-2) for whatever reason. Neither returned the answer you expected. The top pic is from the online calc and the bottom pic from the demo app.

The source for the demo app is in C# so it would need to be converted to VB.Net I suppose but there's online C# to VB.Net converters available. La vida loca

Thursday, July 9, 2015 2:16 AM
• Well that's very nice I suppose and definitely I shall give it a shot. I searched the net though, and realized that I probably should use " NCalc" , which I have no idea about how to call it or should I download and install anything to use it. Here are two pages regarded to this topic:

http://ncalc.codeplex.com/wikipage?title=description&referringTitle=Home

http://stackoverflow.com/questions/28126321/how-to-install-ncalc-in-visual-basic-net-project

(I tried to use it, but the explanation was not adequate and accurate enough )

Abed_4321

Thursday, July 9, 2015 3:29 AM
• Well that's very nice I suppose and definitely I shall give it a shot. I searched the net though, and realized that I probably should use " NCalc" , which I have no idea about how to call it or should I download and install anything to use it. Here are two pages regarded to this topic:

http://ncalc.codeplex.com/wikipage?title=description&referringTitle=Home

http://stackoverflow.com/questions/28126321/how-to-install-ncalc-in-visual-basic-net-project

(I tried to use it, but the explanation was not adequate and accurate enough )

Abed_4321

You will need to place a copy of the NCalc.Dll in some location and then in Visual Studio where you add a reference to a project use Browse to browse to the location of that .Dll in order to add that .Dll as a reference. Then add an Imports statement in your code window in Visual Studio as shown at the stackoverflow link.

You should probably place the .Dll in the folder created for your project so you know where the .Dll is.

You don't provide what your experience level is with Visual Studio or Visual Basic .Net. Or what version of Visual Studio, if any, you are using.

I don't want to write a step by step pictorial procedure. And even if I did it may be wrong for whatever IDE you are using.

La vida loca

Thursday, July 9, 2015 4:14 AM
• Well that's very nice I suppose and definitely I shall give it a shot. I searched the net though, and realized that I probably should use " NCalc" , which I have no idea about how to call it or should I download and install anything to use it. Here are two pages regarded to this topic:

http://ncalc.codeplex.com/wikipage?title=description&referringTitle=Home

http://stackoverflow.com/questions/28126321/how-to-install-ncalc-in-visual-basic-net-project

(I tried to use it, but the explanation was not adequate and accurate enough )

Abed_4321

Will something like this do what you want? Or do you have something else in mind? ```Option Strict On
Public Class Form12

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'(x + A ) * (x + B) = (x^2) + (A+B)x + (A*B)     = foil

Dim a, b, value As Integer

If Integer.TryParse(TextBox1.Text, value) Then
a = value
Else
MsgBox("Invalid Entry")
Exit Sub
End If
If Integer.TryParse(TextBox2.Text, value) Then
b = value
Else
MsgBox("Invalid Entry")
Exit Sub
End If

Label4.Text = "(x ^ 2) + (" & (a + b).ToString & " * x) + " & (a * b).ToString

End Sub
End Class```

• Edited by Thursday, July 9, 2015 4:42 AM value correction
Thursday, July 9, 2015 4:26 AM
• I downloaded and tried NCalc with this "(x-1)*(x-2)" using 4 like the stackoverflow link shows and the result returned was 6. Not a forumula like  (x^2-3*x+2).

La vida loca

Thursday, July 9, 2015 5:01 AM
• As a matter of fact, representing "x" in VB is fairly simple, but  what I will to do is to calculate the coefficient of a pretty difficult function. I could get the final answer for a given "x" (x= a number), but as I'm trying to build the function for a set of numbers, I need to calculate the coefficient of the X's ( X^(a_i) ) independently or just simply do the math operation with a symbolic "x". For example, to see what  (x-1)*(x-2) is gonna be, you can calculate the coefficient of x^2,x and the y-intercept or if you could calculate (x-2)*(x-1) directly.

Regards

Abed_4321

Thursday, July 9, 2015 11:31 AM
• I downloaded and tried NCalc with this "(x-1)*(x-2)" using 4 like the stackoverflow link shows and the result returned was 6. Not a forumula like  (x^2-3*x+2).

La vida loca

Thus, you're suggesting that it is useless in this case? Hence, it's against what it has been claimed. I'm sure there's a way. In Matlab, after any calculation you may use the "sym" command to do your operation symbolically.

Abed_4321

Thursday, July 9, 2015 11:49 AM
• I'm not suggesting anything.

I explained what I did and what the result was. I'm not a Guru of NCalc. That's the first and probably last time I will ever use it.

If NCalc somewhere says something that you think it should do something then learn about it and try it. Don't expect me to read up on it in order to provide answers about it.

NCalc <> Matlab.

La vida loca

Thursday, July 9, 2015 12:32 PM
• As a matter of fact, representing "x" in VB is fairly simple, but  what I will to do is to calculate the coefficient of a pretty difficult function. I could get the final answer for a given "x" (x= a number), but as I'm trying to build the function for a set of numbers, I need to calculate the coefficient of the X's ( X^(a_i) ) independently or just simply do the math operation with a symbolic "x". For example, to see what  (x-1)*(x-2) is gonna be, you can calculate the coefficient of x^2,x and the y-intercept or if you could calculate (x-2)*(x-1) directly.

Regards

Abed_4321

Well I think we can show you how to do it if you show can show us a complete example of what you want to do.

"what I will to do is to calculate the coefficient of a pretty difficult function"

What function? Show us and show the answer and describe the method you want to use. Show why the answer I gave you does not work or is not what you want.

Thursday, July 9, 2015 1:06 PM
• As a matter of fact, representing "x" in VB is fairly simple, but  what I will to do is to calculate the coefficient of a pretty difficult function.

Hi,

what is a pretty difficult function? [Still a polynomial? Or what else?]

Regards,

Thorsten

Thursday, July 9, 2015 1:26 PM
• I'm working on two projects separately, the first one is "Laurent Polynomial" and the other one is "Lagrange Interpolation" with any arbitrary number of given points (I'm a beginner with VB.NET). I could write the code to answer for the both projects for a given "x" (= a number) and now I'm trying add options like presenting the general function and also drawing the figure for Lagrange project.

Regards

Abed_4321

• Edited by Thursday, July 9, 2015 1:39 PM typos
Thursday, July 9, 2015 1:38 PM
• I don't expect you to do that, just asking for help,but thanks anyway. I would share the final result as it will be done.

Abed_4321

Thursday, July 9, 2015 1:41 PM
• As a matter of fact, representing "x" in VB is fairly simple, but  what I will to do is to calculate the coefficient of a pretty difficult function. I could get the final answer for a given "x" (x= a number), but as I'm trying to build the function for a set of numbers, I need to calculate the coefficient of the X's ( X^(a_i) ) independently or just simply do the math operation with a symbolic "x". For example, to see what  (x-1)*(x-2) is gonna be, you can calculate the coefficient of x^2,x and the y-intercept or if you could calculate (x-2)*(x-1) directly.

Regards

Abed_4321

Well I think we can show you how to do it if you show can show us a complete example of what you want to do.

"what I will to do is to calculate the coefficient of a pretty difficult function"

What function? Show us and show the answer and describe the method you want to use. Show why the answer I gave you does not work or is not what you want.

'this program works properly

Public Class Form1
Public myarray As New List(Of Double)
Public myarray2 As New List(Of Double)
Dim n As Integer
Dim x As Double
Dim primaryk As Integer
Dim m As Integer

Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = False
TextBox4.Enabled = False

Button1.BackColor = Color.LightBlue
Button2.BackColor = Color.LightBlue
Button3.BackColor = Color.LightBlue
Button4.BackColor = Color.LightBlue

TextBox1.BackColor = Color.LightPink
TextBox2.BackColor = Color.LightPink
TextBox3.BackColor = Color.LightPink
TextBox4.BackColor = Color.LightPink
TextBox5.BackColor = Color.LightPink

ListBox1.BackColor = Color.Azure

Me.BackColor = Color.DarkOrchid

End Sub

'button2 gets the inputs of our lists and the number of pints
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

m = 0

Static k As Integer = 0
k = primaryk

On Error GoTo error_handler

If n = 0 Then
n = TextBox1.Text
End If

n = n - m

If k < n Then

On Error GoTo error_handler2

TextBox2.Clear()

On Error GoTo error_handler3

TextBox5.Clear()

End If

replace()

primaryk += 1
k = primaryk

If k >= n Then

Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = True
Button4.Enabled = True

Else
Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True

End If
m = n
Exit Sub

error_handler:
MessageBox.Show("Enter valid integer for 'n'")

TextBox1.Clear()
Exit Sub
error_handler2:
TextBox5.Clear()
TextBox2.Clear()

MessageBox.Show("Enter valid number for 'x_i'")

Exit Sub
error_handler3:
TextBox2.Clear()
TextBox5.Clear()
MessageBox.Show("Enter valid number for 'y_i'")
Exit Sub

End Sub

'Total() function does the main math operation
Private Function total() As Double

Dim i, j As Integer
Dim mlt As Double
Dim sum As Double

For i = 0 To myarray.Count - 1

mlt = 1

For j = 0 To myarray.Count - 1

If j <> i Then

'I want to do something like the following without asigning a specificing number to "x"

mlt = mlt * ((x - myarray.Item(j)) / (myarray.Item(i) - myarray.Item(j)))

End If

Next

sum += mlt * myarray2.Item(i)
Next
Return sum
End Function

'button1 get the "x" and does the final calculation stuff
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error GoTo Error_handler4

x = TextBox3.Text

Dim num As Integer

For num = 0 To myarray.Count - 1

Next

TextBox4.Text = total()
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
Exit Sub

Error_handler4:
MessageBox.Show("Enter a valid number for x")
Exit Sub
End Sub

'button3 reset all the inputs

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
myarray.Clear()
myarray2.Clear()
n = 0
m = 0
x = 0
primaryk = 0

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
ListBox1.Items.Clear()

End Sub

'button4 will enable us of entering new number of points
Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()

Dim mymsg As Integer
mymsg = MsgBox("Do you want to use the EX-list?", 3, "Set Points Configuration")
If mymsg = 6 Then

MessageBox.Show("Enter the new value for n")

n = TextBox1.Text
n = n - m
m = n
ElseIf mymsg = 7 Then

Button3.PerformClick()

Else
MessageBox.Show("you suck")
End If
End Sub

'replace() function avoids duplication and caan replace the list's objects
Private Function replace() As Integer

Static index_i As Integer = 0
Dim dup, index_j As Integer

If index_i <> 0 Then

For index_j = 0 To myarray.Count - 2

If myarray.Item(index_i) = myarray.Item(index_j) Then
dup = MsgBox("Duplication is not allowed, Do you want to replace it with the last one?", 4, "Duplication Error")

If dup = 6 Then
myarray.Item(index_j) = myarray.Item(index_i)
myarray2.Item(index_j) = myarray2.Item(index_i)

myarray.RemoveAt(index_i)
myarray2.RemoveAt(index_i)

index_i = index_i - 1

n += 1
Exit For

Else

myarray.RemoveAt(index_i)
myarray2.RemoveAt(index_i)
index_i = index_i - 1
n += 1
Exit For
End If

End If

Next

End If

index_i += 1
Return n

End Function

End Class

Abed_4321

• Marked as answer by Friday, July 17, 2015 3:00 AM
Thursday, July 9, 2015 2:19 PM