C++ to VB.NET Conversion: Assignment Within Expressions - c++

Am converting some C++ code to VB.NET and need to convert assignments within expressions. Below are some C++ lines of code for which it's not clear what the converted results would be:
i2 = 1 + (i1 = i + i)
i4 = 1 + (i3 = n - i1)
wr = (wtemp = wr) * wpr - wi * wpi + wr
data(0) = (h1r = data(0)) + data(1)
data(0) = c1 * ((h1r = data(0)) + data(1))
Would the first line translate to:
If i2 = 1 Then i1 = i + i
?

Hans gave you the procedure - but just in case there's still any doubt about how to do this, your final result should be:
i1 = i + i
i2 = 1 + i1
i3 = n - i1
i4 = 1 + i3
wtemp = wr
wr = wtemp * wpr - wi * wpi + wr
h1r = data(0)
data(0) = h1r + data(1)
h1r = data(0)
data(0) = c1 * (h1r + data(1))

The code is already converted to VB.NET!
For example if you look at the following VB.NET code
Dim i2 As Int16
Dim i1 As Int16
Dim i As Int16
Dim data(0 To 1)
i = 1
i1 = 1
i2 = 0
i2 = 1 + (i1 = i + i) 'Same as your C++ code
MsgBox(i2)
It will return 1. The code translates to
i2 = 1 + (if i1= i+i)

Related

If statement and value of an input variable - Pine Script - Tradingview

I'm having issue with using the value of a variable used as input value, in a if statement Here's a piece of my code :
//#version=3
study(title="v5.0", shorttitle="v5.0", overlay=true)
PP_display = input(1, minval=0, maxval=1)
if (PP_display = 1)
xHigh = security(ticker,"D", high[0])
xLow = security(ticker,"D", low[0])
xClose = security(ticker,"D", close[0])
vPP = (xHigh+xLow+xClose) / 3
vR1 = vPP+(vPP-xLow)
vS1 = vPP-(xHigh - vPP)
vR2 = vPP + (xHigh - xLow)
vS2 = vPP - (xHigh - xLow)
vR3 = xHigh + 2 * (vPP - xLow)
vS3 = xLow - 2 * (xHigh - vPP)
plot(vPP, color=change(vPP) ? na : black, title="vPP", style = linebr, linewidth = width, transp=0)
end if
As a result, I'm getting this error : "syntax error at input 'PP_display'".
I can't find why...
Thanks for your help
If you want to compare PP_display variable with an integer you should use == (equal to) operator. Single = is used to declare variables.
There is no end if in pinescript syntax.
You can't use plot function in the local scope, only in global.
Declaring a variable using the security() function in the local scope will produce a compilation error - Can't call 'security' inside: 'if', 'for'
The solution is to move all your calcs, security calls and plot function to the global scope.
If your intention is to hide the plot with the PP_display input you could use a ternary conditional operator ? : directly in the series argument of the plot function.
//#version=3
study(title="v5.0", shorttitle="v5.0", overlay=true)
PP_display = input(1, minval=0, maxval=1)
xHigh = security(ticker,"D", high[0])
xLow = security(ticker,"D", low[0])
xClose = security(ticker,"D", close[0])
vPP = (xHigh+xLow+xClose) / 3
vR1 = vPP+(vPP-xLow)
vS1 = vPP-(xHigh - vPP)
vR2 = vPP + (xHigh - xLow)
vS2 = vPP - (xHigh - xLow)
vR3 = xHigh + 2 * (vPP - xLow)
vS3 = xLow - 2 * (xHigh - vPP)
plot(PP_display == 1 ? vPP : na, color=change(vPP) ? na : black, title="vPP", style = linebr, linewidth = 2, transp=0)

C++ String ASCII Communication to SerialCOM with VB example

I have a VB 5.0 code that does what I need, but I need to implement it in Visual Studio C++ (2013), the thing is that I'm not getting there, and I have no idea what I'm doing wrong, so I'll show you some code (VB vs C++ - mine) and hope someone is able to help.
For now, thank you for reading this.
I've tried to send the string in very different formats and I think that I finally got it, in how to send, the problem still is reading the answer, I have no idea what I'm doing wrong.
The machine returns (I think 2 bytes) and they are START OF HEADING and ?, I can see it by printing in the console the numbers 1 and 63.
I'll just leave some code.
For asking the current temperature the VB program is:
Private Sub cmdGetTemperaturePV_Click()
If MSComm.PortOpen Then
MSComm.Output = Chr(1) & Chr(0) & Chr(0) & Chr(1) & Chr(13) & Chr(10)
txtMsg(1).Text = "1,0,0,1,13,10"
txtPVTemperature.Text = ""
Else
txtMsg(1).Text = "COM Port OFF"
Beep
End If
End Sub
And mine(C++) is:
String^ a1 = "\x1";
String^ a2 = "\x0";
String^ a3 = "\x0";
String^ a4 = "\x1";
String^ a5 = "\xD";
String^ a6 = "\xA";
String^ enviar = a1 + a2 + a3 + a4 + a5 + a6;
this->serialPort1->Write(enviar);
By using the program "Hercules" I can simulate the reception of the machine and I can see that I'm sending exactly the same thing as the VB program.
Now I think that the problem is receiving, so about that, there's this:
Private Sub tmrRun_Timer()
Dim i As Integer
Dim apt As Byte, B1 As Byte, B2 As Byte
Dim stx As String
If MSComm.PortOpen Then
If MSComm.InBufferCount >= 6 Then
stx = MSComm.Input
stx = Right(stx, 6)
txtMsg(2).Text = ""
For i = 1 To Len(stx)
txtMsg(2).Text = txtMsg(2).Text & Asc(Mid(stx, i, 1)) & ","
Next i
txtMsg(2).Text = Left(txtMsg(2).Text, Len(txtMsg(2).Text) - 1)
apt = Asc(Left(stx, 1))
B1 = Asc(Mid(stx, 2, 1))
B2 = Asc(Mid(stx, 3, 1))
Select Case apt
Case 1:
txtPVTemperature.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 2:
txtSPTemperature.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 3:
Case 4:
txtPVHumidity.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 5:
txtSPHumidity.Text = Format(0.1 * GetInt(B1, B2), "0.0")
Case 6:
Case 7:
Case 8:
Case 9:
If Asc(Mid(stx, 2, 1)) > 0 Then
txtChamber.Text = "ON"
Else
txtChamber.Text = "OFF"
End If
Case 10:
txtEvents.Text = GetInt(B1, B2)
Case 11:
Case 12:
txtInputs.Text = "1..8 = " & B1 & " 9..16 = " & B2
Case 13:
txtAlarms1.Text = " 1.. 8 = " & B1 & " 9..16 = " & B2
Case 14:
txtAlarms2.Text = "17..24 = " & B1 & " 25..32 = " & B2
End Select
End If
End If
End Sub
And I'm trying many different things, the best I got (the one that led me to 1 and 63) is this:
{
String^ rec;
if (this->serialPort1->IsOpen)
{
this->textBox1->Text = String::Empty;
try
{
rec = this->serialPort1->ReadExisting();
}
catch (TimeoutException^)
{
this->textBox2->Text = "Timeout";
}
}
this->textBox1->Text = rec;
char aux[100];
if (rec == String::Empty)
this->textBox2->Text = "String Empty";
else
{
std::string rec1 = marshal_as<std::string>(rec);
strcpy(aux, rec1.c_str());
int a, b, c, d, e1, f;
printf("String received: ");
for (int i = 0; i < 6; i++)
{
if (aux[i] == 0)
break;
printf("%ld ", aux[i]);
if (i == 0)
a = aux[i];
if (i == 1)
b = aux[i];
if (i == 2)
c = aux[i];
if (i == 3)
d = aux[i];
if (i == 4)
e1 = aux[i];
if (i == 5)
f = aux[i];
}
}
}
I'm expecting to receive a 6 byte string and It's not happening.
I'm so sorry for the long post, but I think this way I can be more specific.
Again, thank you very much!

How to Call an expression list in a Block of Mathematica

Here is a block function:
ublock[UU_]:=Block[{tt},U[z_]:=UU[[1]];
tt=2 U[z]+3 U'[z]+U''[z]];
UU:={z^2,z,Sin[z]};
ublock[UU]
Where tt,U[z] are temp variables, and I want to get the result:
2*z^2+6z+2
but the result is:
2z^2
why the results of U'[z] and U''[z] lost?
How to get the result I want?
Some evaluations help
UU := {z^2, z, Sin[z]};
ublock[UU_] := Evaluate#Block[{tt},
U[z_] := Evaluate#UU[[1]];
tt = 2 U[z] + 3 U'[z] + U''[z]];
ublock[UU]
2 + 6 z + 2 z^2
But for more flexibility
Clear[U, UU, ublock]
ublock[UU_] := Block[{tt},
U[z_] := 0;
DownValues[U] = ReplacePart[DownValues[U], {1, 2} -> UU[[1]]];
tt = 2 U[z] + 3 U'[z] + U''[z]]
UU := {z^2, z, Sin[z]}
ublock[UU]
2 + 6 z + 2 z^2
UU = {z^2, z, Sin[z]};
ublock[UU_] := Evaluate#Block[{tt}, U[z_] = UU[[1]];
tt = 2 U[z] + 3 U'[z] + U''[z]];
ublock[UU]
(* 2 + 6 z + 2 z^2 *)
ublock[UU_] := Module[{tt}, U = UU[[1]];
tt = 2 U + 3 D[U, z] + D[U, {z, 2}]]
or better yet
ublock[UU_] := With[{U = UU[[1]]}, 2 U + 3 D[U, z] + D[U, {z, 2}]]

Lotka-Volterra Models in Swift 3

I am trying to implement an rk4 function to solve 2 differential equations. I have this code that implements the Runge Kutta 4 method:
//RK4 method
func rk4_func(y_array: [Double], f_array: [(([Double], Double) -> Double)], t_val: Double, h_val: Double) -> [Double] {
let length = y_array.count
let t_half_step = t_val + h_val / 2.0
let t_step = t_val + h_val
var k1 = [Double](repeating: 0.0, count: length)
var k2 = [Double](repeating: 0.0, count: length)
var k3 = [Double](repeating: 0.0, count: length)
var k4 = [Double](repeating: 0.0, count: length)
var w = [Double](repeating: 0.0, count: length)
var result = [Double](repeating: 0.0, count: length)
for i in 0...length {
k1[i] = h_val * f_array[i](y_array, t_val)
w[i] = y_array[i] + k1[i]/2.0
}
for i in 0...length {
k2[i] = h_val * f_array[i](w, t_half_step)
w[i] = y_array[i] + k2[i]/2.0
}
for i in 0...length {
k3[i] = h_val * f_array[i](w, t_half_step)
w[i] = y_array[i] + k3[i]
}
for i in 0...length {
k4[i] = h_val * f_array[i](w, t_step)
}
for i in 0...length {
result[i] = y_array[i] + (k1[i] + 2.0*k2[i] + 2.0*k3[i] + k4[i])/6.0
}
print(result)
return result;
}
But now I need to actually use it, which is the part I'm confused about. If anyone has experience with numerically computing solutions to differential equations, that would help.
What arrays do I need to feed this function?
What does the t_val argument represent? Is it a maximum time?
How does the output "solve" the equation?
What does the output give me?
In the line k1[i] = h_val * f_array[i](y_array, t_val), what does f_array[i](y_array, t_val) mean? Is it saying that for the i-th value of f_array, find the corresponding i-th value for y_array? Then what does the t_val mean there?
For reference, here are the 2 differential equations needed to be solved. The context is that I'm trying to numerically solve these Lotka-Volterra Models to plot a time series and a phase space plot in Xcode (Swift 3.x).
y is the vector of the current state (implemented as double array). f_array is a function pointer to a function doty = f_array(y,t).
t_val is the time for the current state, h_val is the time step.
One call of rk4_func performs the time step from t_val to t_val+h_val and
returns the new state, y_next = rk4_func(y, f_array, t, h).
One would have to study the language internals. Hopefully, that is, for the code to work correctly, the first call of f_array[0](y_array, t_val) computes the full vector/array-valued result and further calls just extract the components of the cached result.
The original code as found at https://github.com/pdemarest/swift-rk4 is severely deficient in its RK4 realization and out-of-date in language standards. A working version as tested at https://swift.sandbox.bluemix.net/ is
import Foundation
func RK4step(y: [Double], f: ([Double], Double) -> [Double], t: Double, h: Double) -> [Double] {
let length = y.count
var w = [Double](repeating: 0.0, count: length )
var result = [Double](repeating: 0.0, count: length)
let k1 = f(y,t)
assert(k1.count == y.count, "States and Derivatives must be the same length")
for i in 0..<length { w[i] = y[i] + 0.5*h*k1[i] }
let k2 = f(w, t+0.5*h)
for i in 0..<length { w[i] = y[i] + 0.5*h*k2[i] }
let k3 = f(w,t+0.5*h)
for i in 0..<length { w[i] = y[i] + h*k3[i]
}
let k4 = f(w,t+h)
for i in 0..<length {
result[i] = y[i] + (k1[i] + 2.0*k2[i] + 2.0*k3[i] + k4[i])*h/6.0
}
return result;
}
func test_exp(){
// Integrate: y' = y
// y_0 = 1.0
// from 0 to 2.0
var y = [1.0]
func deriv (y: [Double], t: Double) -> [Double] {
return [ y[0] ]
}
var t = 0.0
let h = 0.1
while t < 2.0 {
y = RK4step(y:y, f:deriv, t:t, h:h)
t += h
print("t: \(t), y: \(y[0]) exact: \(exp(t))\n")
}
let exact = exp(2.0)
let error = abs(y[0] - exact)
assert(error < pow(h, 4.0))
print("y: \(y[0]) exact: \(exact) error: \(error)\n")
}
print("testing...\n")
test_exp()
For the Volterra-Lotka dynamics one would have to change
var y = [150.0, 5.0]
let a = 5.0
let b = 1.0
let eps = 0.1
let m = 5.0
func deriv (y: [Double], t: Double) -> [Double] {
let p = y[0]
let q = y[1]
return [ a*p-b*p*q, eps*b*p*q - m*q ]
}
with properly fixed global constants a,b,eps,m and a two-dimensional initial value. Add print statements where required.

VBA EXCEL : Pattern creation function replacing numbers with characters

I have written the below mentioned code but its not functional. Can anyone help?
Explanation:
A 7 or 8 digit number is set. If the number is 8 digits, the first 2 numbers are removed, if the number is 7 digits, the first number is removed. A 6 digit number is left whereby every digit can be repeated without any constraints. So one can have a number between 000001 and 999999. (Zeros on the left are counted).
The code is functional on the first 3 digits but does not function properly later on though i'm using the same logic. The function of the code is to Generate all possible patterns by translating the numbers into characters.
The constraints:
Letters used are only a, b, c, d, e, and f.
Characters should run systematic order
Under this logic:
The pattern can range between aaaaaa and abcdef.
The first character is always "a" and the last character could be "f" in case all digits are different from one another.
So, the number 454657 is translated to abacbd or 123456 is translated to abcdef. (c Can't exist if there is no b and d can't exist if there is no b and c).
Private Sub CommandButton1_Click()
Dim GSM_Counter, GSM, GSM_Range, a, b, c, d, e, f As String
Dim GSM_length, Num1, Num2, Num3, Num4, Num5, Num6, a1, b1, c1, d1, e1, f1 As integer
GSM_Counter = Application.WorksheetFunction.CountA(Range("A:A"))
For i = 2 To GSM_Counter
GSM_length = Len(Range("A" & i))
Select Case GSM_length
Case Is = 8
Range("B" & i) = Left(Range("A" & i), 2)
Num1 = Right(Left(Range("A" & i), 3), 1)
Num2 = Right(Left(Range("A" & i), 4), 1)
Num3 = Right(Left(Range("A" & i), 5), 1)
Num4 = Right(Left(Range("A" & i), 6), 1)
Num5 = Right(Left(Range("A" & i), 7), 1)
Num6 = Right(Left(Range("A" & i), 8), 1)
Case Is = 7
Range("B" & i) = Left(Range("A" & i), 1)
Num1 = Right(Left(Range("A" & i), 2), 1)
Num2 = Right(Left(Range("A" & i), 3), 1)
Num3 = Right(Left(Range("A" & i), 4), 1)
Num4 = Right(Left(Range("A" & i), 5), 1)
Num5 = Right(Left(Range("A" & i), 6), 1)
Num6 = Right(Left(Range("A" & i), 7), 1)
End Select
Range("C" & i) = Num1
Range("D" & i) = Num2
Range("E" & i) = Num3
Range("F" & i) = Num4
Range("G" & i) = Num5
Range("H" & i) = Num6
Next i
For k = 2 To GSM_Counter
a1 = Range("C" & k)
b1 = Range("D" & k)
c1 = Range("E" & k)
d1 = Range("F" & k)
e1 = Range("G" & k)
f1 = Range("H" & k)
a = "a"
Range("K" & k) = a
If b1 = a1 Then
b = "a"
Else
b = "b"
End If
Range("L" & k) = b
If c1 = a1 Then
c = "a"
ElseIf c1 = b1 Then
c = "b"
Else
c = "c"
End If
Range("M" & k) = c
If d1 = a1 Then
d = "a"
ElseIf d1 = b1 Then
d = "b"
ElseIf d1 = c1 Then
d = "c"
Else
d = "d"
End If
Range("N" & k) = d
If e1 = a1 Then
e = "a"
ElseIf e1 = b1 Then
e = "b"
ElseIf e1 = c1 Then
e = "c"
ElseIf e1 = d1 Then
e = "d"
Else
e = "e"
End If
Range("O" & k) = e
If f1 = a1 Then
f = "a"
ElseIf f1 = b1 Then
f = "b"
ElseIf f1 = c1 Then
f = "c"
ElseIf f1 = d1 Then
f = "d"
ElseIf f1 = e1 Then
f = "e"
Else
f = "f"
End If
Range("P" & k) = f
Next k
End Sub
Here is another way..
'~~> Test Data
Sub Sample()
Dim TestArray(1 To 6) As Long
Dim i As Long
TestArray(1) = 468013: TestArray(2) = 12234455: TestArray(3) = 234523
TestArray(4) = 44444444: TestArray(5) = 123: TestArray(6) = 111222
For i = 1 To 6
Debug.Print TestArray(i) & " --> " & Encrypt(TestArray(i))
Next i
End Sub
'~~> Actual Function
Function Encrypt(n As Long) As String
Dim j As Long, k As Long, sNum As String
sNum = Format(CLng(Right(n, 6)), "000000")
j = 97
For k = 1 To 6
If IsNumeric(Mid(sNum, k, 1)) Then
sNum = Replace(sNum, Mid(sNum, k, 1), Chr(j))
j = j + 1
End If
Next k
Encrypt = sNum
End Function
Output
468013 --> abcdef
12234455 --> abccdd
234523 --> abcdab
44444444 --> aaaaaa
123 --> aaabcd
111222 --> aaabbb
EDIT:
If you are planning to use it as a worksheet function and you are not sure what kind of input will be there then change
Function Encrypt(n As Long) As String
to
Function Encrypt(n As Variant) As String
I would suggest getting to know the Chr() and possibly the Asc() VBA functions along with a general knowledge of how digits and alphabetic characters translate to ASCII code characters. I may be reading things wrong but I thought I saw some contradictions between the examples, your description and the actual code provided. Here is one method putting the pattern generation into a User Defined Function or UDF.
Function num_2_alpha(sNUM As String)
'ASCII 0-9 = 46-57, a-z = 97-122
Dim tmp As String, i As Long, c As Long
sNUM = Right(sNUM, 6)
tmp = Chr(97) ' =a
For i = 2 To 6
If CBool(InStr(1, Left(sNUM, i - 1), Mid(sNUM, i, 1))) Then
tmp = tmp & Mid(tmp, InStr(1, Left(sNUM, i - 1), Mid(sNUM, i, 1)), 1)
Else
'tmp = tmp & Chr(i + 96)
c = c + 1
tmp = tmp & Chr(c + 97) 'alternate (code) method
End If
Next i
num_2_alpha = tmp
End Function
Note that I've offered an alternate method that is commented out. Either that line or the one above it should be active; never both at one time. These were the results generated.
             
Addendum: I believe my recent edit should help conform to the examples you left in comments. Code and image updated.