How to let android studio align assignment when reformating the code? - android-studio

Let's assume we have this code:
int x;
double yyyy;
float zzzzzzzz;
x = 1;
yyyy = 24.5d;
zzzzzzzz = 1.3f;
after I enabled:
Settings -> Code Style-> Java -> Wrapping and Braces -> Align Variable in columns
and when I press Ctrl+Alt+L it It is reformatted to the following:
int x;
double yyyy;
float zzzzzzzz;
x = 1;
yyyy = 24.5d;
zzzzzzzz = 1.3f;
Is there any way to make the result like this:
int x;
double yyyy;
float zzzzzzzz;
x = 1;
yyyy = 24.5d;
zzzzzzzz = 1.3f;
with equal indents when assignment multi variables like last three lines.

No way to align the code like that. After five years, there is no way still.

Related

How to parse Int from String.charAt()

I need to parse Integer from first String position.
Something like this:
String s = "1abc";
int x = s.charAt(0);
This doesn't work (obviously) but hopefully you got the idea.
I also can't use anything like this:
int x = s.substring(0, 1);
Since that would return second character ('a') in this case.
For java you could do
int x = Integer.parseInt(s.substring(0,1));
Check if it works

How to calculate integral, numerically, in Rcpp

I've searched for an hour for the methods doing numerical integration. I'm new to Rcpp and rewriting my old programs now. What I have done in R was:
x=smpl.x(n,theta.true)
joint=function(theta){# the joint dist for
#all random variable
d=c()
for(i in 1:n){
d[i]=den(x[i],theta)
}
return(prod(d)*dbeta(theta,a,b)) }
joint.vec=Vectorize(joint)##vectorize the function, as required when
##using integrate()
margin=integrate(joint.vec,0,1)$value # the
##normalizeing constant at the donominator
area=integrate(joint.vec,0,theta.true)$value # the values at the
## numeritor
The integrate() function in R will be slow, and since I am doing the integration for a posterior distribution of a sample of size n, the value of the integration will be huge with large error.
I am trying to rewrite my code with the help of Rcpp, but I don't know how to deal with the integrate. Should I include a c++ h file? Or any suggestions?
You can code your function in C and call it, for instance, via the sourceCpp function and then integrate it in R. In alternative, you can call the integrate function of R within your C code by using the Function macro of Rcpp. See Dirk's book (Seamless R and C++ Integration with Rcpp) on page 56 for an example of how to call R functions from C. Another alternative (which I believe is the best for most cases) is to integrate your function written in C , directly in C, using the RcppGSL package.
As about the huge normalizing constant, sometimes it is better to scale the function at the mode before integrating it (you can find modes with, e.g., nlminb, optim, etc.). Then, you integrate the rescaled function and to recover the original nroming constant multiply the resulting normalizing constant by the rescaling factor. Hope this may help!
after reading your #utobi advice, I felt programming by my own maybe easier. I simply use Simpson formula to approximate the integral:
// [[Rcpp::export]]
double den_cpp (double x, double theta){
return(2*x/theta*(x<=theta)+2*(1-x)/(1-theta)*(theta<x));
}
// [[Rcpp::export]]
double joint_cpp ( double theta,int n,NumericVector x, double a, double b){
double val = 1.0;
NumericVector d(n);
for (int i = 0; i < n; i++){
double tmp = den_cpp(x[i],theta);
val = val*tmp;
}
val=val*R::dbeta(theta,a,b,0);
return(val);
}
// [[Rcpp::export]]
List Cov_rate_raw ( double theta_true, int n, double a, double b,NumericVector x){
//This function is used to test, not used in the fanal one
int steps = 1000;
double s = 0;
double start = 1.0e-4;
std::cout<<start<<" ";
double end = 1-start;
std::cout<<end<<" ";
double h = (end-start)/steps;
std::cout<<"1st h ="<<h<<" ";
double area = 0;
double margin = 0;
for (int i = 0; i < steps ; i++){
double at_x = start+h*i;
double f_val = (joint_cpp(at_x,n,x,a,b)+4*joint_cpp(at_x+h/2,n,x,a,b)+joint_cpp(at_x+h,n,x,a,b))/6;
s = s + f_val;
}
margin = h*s;
s=0;
h=(theta_true-start)/steps;
std::cout<<"2nd h ="<<h<<" ";
for (int i = 0; i < steps ; i++){
double at_x = start+h*i;
double f_val = (joint_cpp(at_x,n,x,a,b)+4*joint_cpp(at_x+h/2,n,x,a,b)+joint_cpp(at_x+h,n,x,a,b))/6;
s = s + f_val;
}
area = h * s;
double r = area/margin;
int cover = (r>=0.025)&&(r<=0.975);
List ret;
ret["s"] = s;
ret["margin"] = margin;
ret["area"] = area;
ret["ratio"] = r;
ret["if_cover"] = cover;
return(ret);
}
I'm not that good at c++, so the two for loops like kind of silly.
It generally works, but there are still several potential problems:
I don't really know how to choose the steps, or how many sub intervals do I need to approximate the integrals. I've taken numerical analysis when I was an undergraduate, I think maybe I need to check my book about the expression of the error term, to decide the step length.
I compared my results with those from R. the integrate() function in R can take care of the integral over the interval [0,1]. That helps me because my function is undefined at 0 or 1, which takes infinite value. In my C++ code, I can only make my interval from [1e-4, 1-1e-4]. I tried different values like 1e-7, 1e-10, however, 1e-4 was the one most close to R's results....What should I do with it?

How to sum up elements of an arraylist having type String?

I have an arraylist consisting of decimal numbers and i m supposed to take the average of last 4 elements of this arraylist.And these rational number are type of String.
private void average(String confidence) {
if(myList.size() >= 4) {
String t = myList.get(myList.size()-1);
String d = myList.get(myList.size()-2);
String f = myList.get(myList.size()-3);
String h = myList.get(myList.size()-4);
String s = (t + d + f+h) ;
long fin = Long.parseLong(s);
long result = fin/4 ;
System.out.println("Average is: "+result);
}
but this method does not work.Could you please tell me what kind of changes am i supposed to do or any advices of doing this? Thanks a lot in advance!!!
Your issue is the String s = (t + d + f+h) part. You're just appending 4 Strings right now.
You need to convert them first.
And your result will be wrong, you need to divide by 4, not 3.
Colleen's answer is good, so long as you remember to change the type to reflect what Long.parseLong() returns. I'd reply, but I'm too new.
if(myList.size() >= 4) {
Double t = Double.parseLong(myList.get(conf.size()-1));
Double d = Double.pareseLong(myList.get(conf.size()-2));
Double f = Double.parseLong(myList.get(conf.size()-3));
Double h = Double.parseLong(myList.get(conf.size()-4));
Double result = (t + d + f + h) / 3 ;
System.out.println("meanAsrConfidence is: "+result);
}
You need to convert before you add. + on Strings means concatenate.
if(myList.size() >= 4) {
//I'm guessing all of these conf.size() calls are meant to be myList.size()
double t = Double.parseDouble(myList.get(conf.size()-1));
double d = Double.pareseDouble(myList.get(conf.size()-2));
double f = Double.parseDouble(myList.get(conf.size()-3));
double h = Double.parseDoublemyList.get(conf.size()-4));
double s = (t + d + f+h) ;
double result = s/3 ; //should be 4
System.out.println("meanAsrConfidence is: "+result);
}
What you are doing now is, for example:
t=1, d=2, f=3, h=4
s = 1234
Also:
you need to divide by 4, not 3
what is conf? Why are you not calling myList.size()?
you're passing a String as an argument when you probably want to be passing myList

Difference between int and double in C++ Visual Studio

I am making a simple program which abstracts complex numbers and complex number operations. I started out with integer data types for my imaginary and real aspects of my complex numbers because of my vast ignorance when, after coding addition, subtraction and multiplication successfully, I realized that for division I would need to use doubles. When I switched to doubles I got bad results from my previous three calculations which worked wonderfully when the values were stored as ints. Can someone please explain to me what is so fundamentally different about ints and doubles in c++ that makes my code work fine for int but die when I try using doubles?
I have pasted my code for reference.
#include "Complex.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Complex::Complex(){
real = 0;
imaginary = 0;
}
Complex::Complex(double givenReal, double givenImaginary)
{
real = givenReal;
imaginary = givenImaginary;
}
double Complex::getImaginary(){
return imaginary;
}
double Complex::getReal(){
return real;
}
double Complex::getMagnitude(){
//magnitude = sqrt(pow(real,2)+pow(magnitude,2));
return magnitude;
}
Complex Complex::operator+(Complex n){
Complex j = Complex();
j.real = real + n.real;
j.imaginary = imaginary + n.imaginary;
return j;
}
Complex Complex::operator-(Complex n){
Complex j = Complex();
j.real = real - n.real;
j.imaginary = imaginary - n.imaginary;
return j;
}
Complex Complex::operator*(Complex n){
Complex j = Complex();
j.real = (real * n.real)-(imaginary * n.imaginary);
j.imaginary = (real * n.imaginary) + (imaginary * n.real);
return j;
}
Complex Complex::operator/(Complex n){
Complex j = Complex();
j.real = ((real * n.real) + (imaginary * n.imaginary))/(n.real*n.real + n.imaginary*n.imaginary);
j.imaginary = ((imaginary*n.real)-(real * n.imaginary))/(n.real*n.real + n.imaginary*n.imaginary);
return j;
}
int main(){
Complex a = Complex(1, 3);
Complex b = Complex(4, 8);
Complex c = a+b;
printf("Adding a and b\nExpected: (5,11)\nActual: (%d,%d)\n",c.getReal(), c.getImaginary());
c = a-b;
printf("Subtracting b from a\nExpected: (-3,-5)\nActual: (%d,%d)\n",c.getReal(), c.getImaginary());
c = a*b;
printf("Multiplying a and b\nExpected: (-20,20)\nActual: (%d,%d)\n",c.getReal(), c.getImaginary());
c = a/b;
printf("Dividing a by b\nExpected: (.35,.05)\nActual: (%d,%d)\n",c.getReal(), c.getImaginary());
system ("pause");
}
Output:
Adding a and b
Expected: (5,11)
Actual: (0,1075052544)
Subtracting b from a
Expected: (-3,-5)
Actual: (0,-1073217536)
Multiplying a and b
Expected: (-20,20)
Actual: (0,-1070333952)
Dividing a by b
Expected: (.35,.05)
Actual: (1610612736,1071015526)
What every C/C++ programmer should know about printf format specifiers are: %d is for int, %f is for double.

How to make Resharper place blank lines after { in c#

Does anyone know how to make Resharper add blank lines after brackets when performing code cleanup?
I want to make the following code
if (x == y)
{
int a = x;
int b = y;
int z = a + b;
}
Look like this
if (x == y)
{
int a = x;
int b = y;
int z = a + b;
}
I have had a look through the Resharper options but can't see anything to add the extra blank lines around your block of code, although I am not sure that format makes it any more readable than without extra spaces. I would suggest asking this question on the Resharper News Group: news://news.jetbrains.com/jetbrains.resharper.community

Resources