Scalastyle rule for line breaks between arguments - scala

The scalastyle website mentions splitting the arguments in multiple lines if they exceed a certain length. The problem I am facing is that I need to enforce a rule that splits the arguments in class/method definitions as well as invocation into multiple lines regardless of the length. For example,
doX(x, y, z) or
doX(x,y,
z)
should become
doX(x,
y,
z)
How do I reach this point? Is there an existing rule that can be configured or do I need to write custom rule? Also, is there a better alternative e.g. scalafmt that could address this?
Edit: The arguments are aligned as well.

Related

How to trace out a curve

I am working on a project dealing with closed curves. I want to trace out a curve swept out by a coordinate vector moves. Just to get the code down I'm trying to accomplish the goal using a circle. I am able to animate the motion of the vector with the following command
animate(arrow, [[cos(2*Pi*n/1000),sin(2*Pi*n/1000)], shape = arrow,
scaling = constrained], n=0..1000, frames = 100);
Is there a way to trace the circle that is swept out by this curve. Again my goal is to be able to do this for an arbitrary parameterized curve. Any help is greatly appreciated.
Here is a basic but verbose way to do it,
restart;
plots:-animate(plots:-display,
[ 'plots:-arrow([cos(2*Pi*n/1000),
sin(2*Pi*n/1000)],
shape = arrow)',
'plot([cos(2*Pi*nn/1000),
sin(2*Pi*nn/1000),nn=0..n])',
scaling = constrained ],
n=0..1000, frames = 30);
If that seems complicated then perhaps it's good to review Maple's evaluation rules for procedure calls. Maple normally evaluates the arguments before passing them to the procedure.
But sometimes you don't want that evaluation to occur until, say, the procedure can provide a numeric value for a parameter in some argument. That is to say, sometimes you want to avoid premature evaluation of the arguments.
The seq command deals with this by having so-called special-evaluation rules, where the evaluation of the argument is delayed unto the seq index variable takes on it individual values.
The plots:-animate command allows you to deal with it by separating the main command from its arguments (which get passed separately, in a list). That is often adequate, but not when those arguments in the list also contain full calls to plotting commands which would not evaluate ok (ie. without error, up front) until the animating parameter gets its actual values.
That is why I also used single right-quotes to delay the evaluation of the calls to plots:-arrow and plot in the example above. Those evaluations need to wait until the animating parameter n takes on its actual numeric values.
And another, related approach is to create a procedure which accepts the animating parameter value and constructs a whole frame.
F := proc(n)
plots:-display(
plots:-arrow([cos(2*Pi*n/1000),
sin(2*Pi*n/1000)],
shape = arrow),
plot([cos(2*Pi*nn/1000),
sin(2*Pi*nn/1000),
nn=0..n]),
scaling = constrained);
end proc:
This can be handy as it lets you test beforehand.
F(307.2);
(I didn't bother to optimize F, but you could notice that the sin and cos calls happen twice, and instead do them just once inside the procedure and assign to local variables. That might make things easier when you go on to more involved parametric curves.)
Now the calls to plots:-animate can be terse,
plots:-animate(F, [ n ],
n=0..1000, frames = 30);
The above produces the same animation as before.
Here's another way, by constructing a list containing a sequence of all the frames.
Note that, as written, evaluating F at unknown, unassigned name n produces an error.
F(n);
Error, (in plots/arrow) invalid input: plottools:-arrow
expects its 3rd argument, pv, to be of type {Vector, list,
vector, complexcons, realcons}, but received 0.5000000000e-1*
(cos(0.6283185308e-2*n)^2+sin(0.6283185308e-2*n)^2)^(1/2)
The error occurs because n does not have a numeric value.
But the special-evaluation rules of the seq command allow us to proceed anyway, since it delays the evaluation of F(n) until n gets its values.
# note termination with full colon, to suppress output
S := [seq(F(n), n=0..1000, (1000-0)/(30-1))]:
nops(S); # check that we really got 30 frames
30
plots:-display(S, insequence=true);
That last command also displays the same 30-frame animation.

Define Nonconstant Boundary Conditions "partially" on an edge - MATLAB

I need to define Dirichlet and Neumann conditions in a plain stress problem only on part of one of the edges of a plate.
Matlab's help for defining nonconstant boundary conditions indicates that functions must be written such as:
applyBoundaryCondition(model,'edge',1,'r',#myrfun);
applyBoundaryCondition(model,'face',2,'g',#mygfun,'q',#myqfun);
applyBoundaryCondition(model,'edge',[3,4],'u',#myufun,'EquationIndex',[2,3]);
It moreover says that each function must have the following syntax.
function bcMatrix = myfun(region,state)
and finally that "region" is a structure containing the fields region.x (x-coordinate of the points), region.y (y-coordinate of the points), etc. if there are Neumann conditions (which is my case), then solvers pass the following data in the region structure: region.nx — (x-component of the normal vector at the evaluation points), etc. My questions are:
From where can I get the structure region?
How can I pass the argument that boundary conditions apply in one part of one of the edges?
Thanks!!
#Oliver,
1) I think you don't need to make the structure region, but make functions that are capable of using it. Since the boundary condition generally depends on the location, you will need region.x and region.y.
2) You can use region.x and region.y to make boundary conditions dependent on the location. This is one way of applying them "partially" if the type of boundary condition is the same. Otherwise, you will have to define the split in the boundary explicitly. This happens probably while defining the geometry of the problem.

How to allow multiple syntax options in MATLAB function calls

I am writing a set of customized plot functions in MATLAB, whose aim is to ensure a more consistent look of our company charts and to be fast.
In these customized functions, I would like to reproduce MATLAB's variability in allowing multiple syntax options for function calls, similar to the syntax options available for MATLAB's plot function.
So, say my function is plotFun.m, then the following function calls should all work:
plotFun(y)
plotFun(x,y)
plotFun(ax,_)
plotFun(_,Name,Value)
I am familiar with inputParser and several other techniques to parse name-value pairs passed as varargin, yet I cannot figure out how to parse the first arguments, that is ax, x and y...
I have scanned the file exchange and found several contributions for parsing name-value pairs (somehow overlapping with the inputParser functionality), but none of them seems to do the trick in this case.
EDIT:
In reply to thewaywewalk 's comment, here are some more details regarding what the syntax options should do.
Suppose plotFun does something similar as the line function (but with other defaults and a different set of allowed options).
y should be validated to be a numerical vector
x (if present) should be validated to be a numerical or datetime vector
plotFun(x,y)
If x is of class datetime, x = datenum(x), then
call line(x,y, 'Parent',gca)
plotFun(y)
should set x = 1:numel(y), then call line(x,y, 'Parent',gca) as above
plotFun(ax,_)
should do the same as above, but call line(x,y, 'Parent',ax) instead of gca
plotFun(_,Name,Value)
should parse and validate the name-value pairs and pass them to the call of line. I solved this task with the help of inputParser.
EDIT 2
In the world of my dreams, hitting Ctrl + F1 for plotFun would show the function hints, that is, a list of allowed syntax options (like the one I typed in the code block above), as opposed to showing a more cryptic plotFun(varargin)...

Unbounded (infinite) repetitions in transitions for covergroup bins

How can I define coverage bin for transition that might have many repetitions in it? What I'm trying to do is something like this:
bins st = (2=> 3[* 1:$] => 2);
Of course, this doesn't work. Simulators give error messages when compiling this line.
Edit: Simulators complain about $ symbol, they don't recognize it as "unbounded maximum". However when writing sequences, it is legal to use consecutive repetition operator as [* 1:$]. I hope next version of SystemVerilog makes it legal for covergroups too.
As a crude workaround, I substituted $ with a large number so it works fine for my case.
bins st = (2=> 3[* 1:1000] => 2);
SystemVerilog transition bins were not designed to handle anything but simple transitions. Anything more complex should be modeled using a cover directive, or some combination of sequence.triggered() method and covergroup.

Turn off "smart behavior" in Matlab

There is one thing I do not like on Matlab: It tries sometimes to be too smart. For instance, if I have a negative square root like
a = -1; sqrt(a)
Matlab does not throw an error but switches silently to complex numbers. The same happens for negative logarithms. This can lead to hard to find errors in a more complicated algorithm.
A similar problem is that Matlab "solves" silently non quadratic linear systems like in the following example:
A=eye(3,2); b=ones(3,1); x = A \ b
Obviously x does not satisfy A*x==b (It solves a least square problem instead).
Is there any possibility to turn that "features" off, or at least let Matlab print a warning message in this cases? That would really helps a lot in many situations.
I don't think there is anything like "being smart" in your examples. The square root of a negative number is complex. Similarly, the left-division operator is defined in Matlab as calculating the pseudoinverse for non-square inputs.
If you have an application that should not return complex numbers (beware of floating point errors!), then you can use isreal to test for that. If you do not want the left division operator to calculate the pseudoinverse, test for whether A is square.
Alternatively, if for some reason you are really unable to do input validation, you can overload both sqrt and \ to only work on positive numbers, and to not calculate the pseudoinverse.
You need to understand all of the implications of what you're writing and make sure that you use the right functions if you're going to guarantee good code. For example:
For the first case, use realsqrt instead
For the second case, use inv(A) * b instead
Or alternatively, include the appropriate checks before/after you call the built-in functions. If you need to do this every time, then you can always write your own functions.