Is OpenMP (parallel for) in g++ 4.7 not very efficient? 2.5x at 5x CPU

Question: I've tried using OpenMP with a single #pragma omp parallel for, and it resulted in my programme going from a runtime of 35s (99.6% CPU) to 14s (500% CPU), running on Intel(R) Xeon(R) CPU E3-1240 v3 @ 3.40GHz.  That's the difference between compiling with g++ -O3 and g++ -O3 -fopenmp, both with gcc (Debian 4.7.2-5) 4.7.2 on Debian 7 (wheezy).

Why is it only using 500% CPU at most, when the theoretical maximum would be 800%, since the CPU is 4 core / 8 threads?  Shouldn't it be reaching at least low 700s?
Why am I only getting a 2.5x improvement in overall time, yet at a cost of 5x in CPU use?  Cache thrashing?

The whole programme is based on C++ string manipulation, with recursive processing (using a lot of .substr(1) and some concatenation), where said strings are continuously inserted into a vector of set.
In other words, basically, there are about 2k loop iterations done in a single parallel for loop, operating on vector, and each one of them may do two recursive calls to itself w/ some string .substr(1) and + char concatenation, and then the recursion terminates with set .insert of either a single string or a concatenation of two strings, and the said set .insert also takes care of a significant number of duplicates that are possible.
Everything runs correctly and well within the spec, but I'm trying to see if it can run faster. :-)
Comment: Can you provide an [mcve]? There are many important bits missing from your description, most importantly the size of the working set. Is the `set` shared between threads? Have you confirmed your `OMP_NUM_THREADS` environment variable setting?
Comment: @Zulan, no, each iteration of the `for` loop has its own `set` (otherwise, I doubt it'll even work, since it wouldn't be thread-safe), which are all part of a `vector` structure.  there is no intentional sharing of anything between the threads; certainly no sharing of any mutable objects.
Comment: ok, funny story -- i changed my recursive function to have all the string input as `const`, and speed went up from 35s to 27s uniprocessor (finally matching my golang and erlang implementations), and 12s on mp; however, that's still only a 2.5x improvement at 5x resources.
Comment: well, i think you guys are right to dismiss hyperthreading -- i did `schedule(static) num_threads(4)` at the end of that pragma, and i'm now reliably getting below 12s and at only 320% (e.g., actually a second faster than the prior `const` optimisation, which could run as fast as 11.3s or as slow as 13s, depending on its mood); is there a way to automatically detect hyperthreading, and spin up only half the threads?  for what it's worth, i think the whole contention resolves around automatic memory allocation.
Comment: You can certainly [detect Hyper Threading](http://stackoverflow.com/q/2901694/620382), but it is more reliable to control the threading with a system and compiler specific configuration.  Often you also want to consider affinity settings.
Comment: Wow, so, I now tried `schedule(dynamic) num_threads(5)`, and i'm reliably getting 9.8s 474%!  That's 27/9.8 = 2.75x speedup, at 4.7x CPU!  The speed goes above 10s with a higher number of threads, and is 10.0s at 4 threads.  Anyhow, there's a followup at http://stackoverflow.com/questions/36958661/openmp-dont-use-hyperthreading-cores-half-num-threads-w-hyperthreading.
Answer: Based on your description you can draw the following conclusions:
I am assuming OpenMP really uses 8 threads (verify by export OMP_NUM_THREADS=8)

500% CPU means, that there is significant time spent in barriers. This is due to bad load balance: different iterations taking varying amount of time. Hence the default (static) loop scheduling is inefficient, try different kinds of loop scheduling, e.g. dynamic.
If the runtime time does not decrease proportionally to the number of threads (e.g. the overall CPU time spent increases), there is either a shared resource that acts as a bottleneck, or an influence between the threads. Note this can also be the result of short barriers (from load imbalances), that perform busy waiting instead of blocking.

The most common shared resource is the memory bandwidth. Whether that affects you, depends on whether your working set fits into local caches. Considering the many memory hierarchies and NUMA properties in modern systems, this can become very complex to understand. The is nothing fundamental you can do against that short of restructuring your data accesses to use caches more efficiently (blocking).
False sharing: If multiple threads write & read to the same memory locations (cache lines), the respective memory accesses become much slower. Try to restrict writes within the parallel loop to private variables.
HyperThreading - a core being a shared resource between two hardware threads.

using 5x resources

This is a fundamental misunderstanding. The additional hardware thread provides little additional resources to each core. If two threads run on the same core, they will share computational resources and memory bandwidth, basically the only advantage is hidden I/O latency. You will never see 2x speedup, despite 2x CPU time. Sometimes it will give you 10% speedup, sometimes it will be slower.
Now consider 5 active threads running on 4 cores. 2 threads, sharing a core, will run only at ~50% speed, slowing everything down. Try reducing the number of threads to the number of cores (export OMP_NUM_THREADS=4).
Comment: Again,  CPU % does not correspond to *resource use*.
Comment: interestingly, i've tried `schedule (dynamic)`, and I am getting 760% CPU, but the time the programme runs is still the same (actually, half a second slower), so, it's even worse -- 2.5x speedup at 8x resource use.
Answer: Achieving linear speedup is usually not trivial, and it gets harder the more cores you have. There might be several aspects you may need to look for:

False sharing: if the array is not sliced properly, a cache line may be in contention between two cores, usually the two halves of the cache line are written by two threads causing the cache line to move from the L2 cache of one core to the other. Cache sharing can also happen if you are using lots of shared variables. In that case consider using private or firstprivate to avoid synchronization.
OpenMP scheduling: if not specified, OpenMP will split the iterations of the loop in equal manner among the threads in the system and assign a subrange to each of them. However, if the amount of work for each index vary then you may end-up in a situation where most of the threads are finish their work and they are blocked at the barrier at the end of the parallel region waiting for the slower thread (the one that got more work to do). This depends on the type of algorithm you have implemented inside your loop, but OpenMP gives you the opportunity to change scheduling policy using the schedule() directive. Consider trying dynamic at least.
Comment: I think you misread my question -- i'm only seeing a 2.5x speedup (using 5x resources), which is certainly better than nothing, but after the initial euphoria, does seem to leave quite some room for further improvement.
Comment: I would try increase the size of the loop and see if your speedup improves. If not, it might be your program is IO-bound which means it is mostly waiting fetching stuff from memory and it does not have a good cache behavior.


Transposing on a guitar

Question: I play flutes, whistles, and the alike in sessions and have recently got myself a decent guitar to start my journey. Whistles are transposing instruments so if I am playing a whistle in the key of D, somebody could put a capo on the 2nd fret, use the same chord shapes that would be needed for an instrument in C and it would sound good.
Am I correct so far?
If I am, what would I do, guitar wise, if I wanted to play along with somebody who was playing a whistle in the key of Bb? I've seen many people play along with a whistle with a capo on the 3rd fret, or 2nd, 1st, why?, what are they achieving by doing this?
Would they have to know different chords for each fret? Is there no 'easy' way of just playing along with somebody in a session if you knew the shape of the chords on a guitar which did not have a capo?
Does this make sense?
Answer: For guitar players the keys E, A, D, G, C (and relative minors/modes) are considered "easy" keys -- the common, beginer open chords fall in these keys.  And especially for acoustic guitars, the ringing of open strings in these keys is a part of the guitar's tonal quality, and is desireable for some styles of music.
So what do you do when you want to play in B flat?

Use barre chords as indicated in Dom's answer, but then you loose some of the strummy goodness that open chords provide,
Capo 1, and play as if in A, using the A,D,E chord shapes for I-IV-V -- note that you can get the relevant open strings to ring out
Capo 3, and play as if in G -- this will have different chord voicings and allow for different decorations that the Capo 1 option.

You already understand the logic of what the capo does, so it's just the idea that these keys are the easy strumming keys for guitars.
One other consideration is that it is less common to capo the guitar at above the fifth fret or so -- it is easier to get into tuning/intonation problems with the capo that high up the neck.
Comment: "some styles of music" would include Irish traditional music, which I suspect might be relevant here considering the asker's references to whistles, flutes and sessions.
Answer: A guitar in standard tuning is actually a good fit for the key of G major (E minor) since the you can take advantage of some basic open chords in the key of G major which are G(I), Am(ii), C(IV), D(V), and Em(vi) as shown below:
G  Am  C  D  Em

If you were to put a capo on the 2nd fret you would be able to use the same shapes to make the same relative chords as shown above in the key of A major (F# minor) which are A(I), Bm(ii), D(IV), E(V), and F#m(vi). It makes transposing a lot easier for playing chords because you only have to think the open shape in the key.
This however is unnecessary because the on the guitar you can play chords in many different places especially if you learn barre chords. For example you can play the chords mentioned the following way:
A Bm D E F#m

In the example above the F#m is an example of an Em shape barre chord and the Bm is an example of an Am shape barre chord. If you learn all five barre chord shapes you can play in any key without a capo. It's not easy, but it's a lot more rewarding.
Answer: Adding to Dave's answer, specifically playing along with a Bb instrument,
*capo 1st fret, use 'open' A, D and E, or E7.
*capo 3rd fret, use 'open' G, C and D, or D7.
*capo 6th fret, use 'open' E, A and B7.
Slight problems for a beginner would be - in the first case, F#m, Bm and C#m. In the second - Bm, and the third - F#m, G#m and C#m, as all those chords ideally need to be barred. Of course, those barre chords may not be needed in particular songs.
Learning to play barre chords will save the day, but as Dave points out, the brightness of open chords tends to be less, especially using an acoustic guitar.
Other fret options may be available for the capo, but it obviously depends what key the whistle, etc is in.
Answer: You could also tune the guitar down a half step to get the Natural open Bb / Eb and Ab. Some of the open chords may be different then but it is an option.
Answer: I'm guessing since you mention sessions and whistles that your major focus is on Irish or Scottish music.  With a D whistle you'd be playing mostly in D and G, I think you'll find the best results to capo 3 and play in G positions for Bb (equivalent of D in a D whistle) and stay on capo 3 and play in C position for Eb (equivalent of playing in G on a D whistle). You can then play in D position for F (equivalent of playing in A on a D whistle)
Good luck!
Answer: Kudos for expanding your musical horizons by learning the guitar.  I find it to be one of the most versatile musical instruments available.
A capo is not required to play a song on guitar in any key you desire but will make it much easier to play in certain keys.  The way a guitar and similar fretted stringed instruments are set up makes it difficult to play certain chords in certain keys.  Many professional  musicians even use a capo - although some very accomplished and experienced guitarist consider the use of a capo akin to riding a bicycle with training wheels.
But as a beginning guitar student, you will find that the capo is your friend and will greatly reduce the learning curve.
I feel like the chord formations for playing guitar in the key of G are the easiest for a beginning student to learn.  So if you learn the G, C, and D major chords and the Em and Am chords which can all be played as open chords, you will be able to play most songs in the key of G. Later you can add the Bm Barre chord to play even more songs.  Once you learn these chord formations, you can transpose to other keys simply by using a capo and playing those same chord formations.  Put the capo on the 2nd fret and play the same chords - now you are in the key of A. Capo third fret = Bb, fourth fret = B, fifth fret = C and so on.  Unfortunately things start getting tight past the 7th fret.
But after you learn the first position chords to play guitar in the key of G, the next easiest to play for most beginning guitar students would be the chords in the key of C.
But - to play the chords in the key of C you must master the dreaded F chord.  It's difficult mainly because it involves barring either 2 or 6 strings (depending on if you play the 4 string version or full barre chord) on the first fret which is harder than the second fret or third fret because the nut raises the strings above the fret board and that close to the nut it is harder to press the strings down to the frets.  The good news though, is that using a capo to transpose to other keys from C, actually makes the F chord shape easier to play than without the capo.  This is because the capo lowers the height of the strings down to the fret it's behind and thus closer to the fret board than the height of the strings at the nut.
If you learn the basic first position chords for the key of G and C (most are open chords), the capo will allow you to play in all the major keys with just those chords.
As you continue learning chords in other keys, the capo will allow you to achieve different nuances for songs in a given key by using the capo and different chord formations to alter the voicing used for the chords.
Or you may find certain transitions between chords in a particular song easier to play using the chords of G versus the chords of A for example -  so you could put a capo on the second fret and play as if in G - even though you may be capable of playing the chords in the key of A.
Beyond making certain keys much easier to play in, the capo also gives you more options - even as you become more experienced and expand your chord vocabulary.
Enjoy your journey.

Transposing on a guitar


sparkSQL Map column in group by clause

Question: I am migrating a query from Hive to SparkSQL, but, running into one issue with Map column.
My query is
<code>spark.sql(select col1,col2,my_map,count(*) from table group by col1,col2,my_map)
</code>
The error I get is
<code>`my_map` cannot be used as a grouping expression because its data type map<string,string> is not an orderable data type.;
</code>
The keys in my_map always vary. I tried using deprecated HiveContext, but, that didn't help. Is there any workaround for this?
Thanks!
Comment: grouping by a map seems a rather delicate operation. Are you sure you really want to do this?

Anyway, you can think about normalizing the map before doing this query (especially if you expect only a few keys), otherwise you should transform it back and forth to another format that can be easily handled by sql.
Comment: a list of tuples may be an option, I believe it should work. But be careful, this is really close to a dirty work around
Comment: @Chobeat I have been doing it in Hive up until now. Any idea how I could transform it back and forth to another format ?
Comment: @Chobeat How do I convert a Map column from to a list of tuples and convert it back to map?
Answer: The answer is in the error response. You need to make my_map into an orderable data type! :)

sparkSQL Map column in group by clause


Prove that: $e^{-\frac{|t|}{2}}= \int_{\mathbb R} \frac{e^{ixt}}{1+x^2} dx.$

Question: I need help for prove that
$$e^{-\frac{|t|}{2}}= \int_{\mathbb R} \frac{e^{ixt}}{1+x^2} dx, \quad \forall t\in \mathbb R.$$
Thank you in advance
Comment: The RHS ia actually $\pi e^{-|t|}$.
Comment: Can you prove that the value of the integral is real? (Hint: apply $e^{ix} = \cos x + i\sin x$.)
Comment: this has been asked before
Comment: @qbert Can you link to the previous iteration? That makes it easier to vote to close.
Comment: See https://math.stackexchange.com/q/846961/321264.
Answer: Hint:
If $t\geq 0$, examine
$$
\int_{\Gamma_R} \frac{e^{izt}}{1+z^2}\mathrm dz
$$
using the usual contour $\Gamma_R$ (semicircle union the real axis) in the upper half plane.
If $t<0$, examine
$$
\int_{\Gamma_R'} \frac{e^{izt}}{1+z^2}\mathrm dz
$$
where now $\Gamma_R'$ is the contour $\Gamma_R$ reflected across the real axis.
This will insure that the contribution of the arc is 0 in either case.
Comment: "This has been asked before", you noted.  Then you answer anyway.
Comment: @amWhy I could not find the previous iteration and offered a hint on the chance that OP wants a hint, rather than a fully worked answer. If you feel strongly, I would be happy to delete it.


Not sure why I get Uncaught TypeError

Question: What I am trying to accomplish with this is to have a URL for the API accessible via Drupal.settings so I can get to it from PhP and js with Drupal 7.41.
I have put this at the top of my custombootstrap.module file:
function yourmodule_init(){
    $apiurl="http://dev3.ws.mysite.com/api"
    drupal_add_js(array('yourmodule' => array('myurl' => $apiurl)), array('type' => 'setting'));
    variable_set('myurl',$apiurl);
}

In a file called mapLayer.js I use the code like this:
var List_Users_Map = function(){
var userId = $('#uid-loged').val();
$('#list-users-gis-map').html('');
//linkedRequests(userId);   // RCD- CAll
$.ajax({
    url: Drupal.settings.yourmodule.myurl+'linked_users_gis/x-api-key/blahhhhhhhhhhTq4SWuCiLONePNfschNft',
    data:{uid:userId},
    type: 'GET',
    ...

Uncaught TypeError: Cannot read property 'myurl' of undefined can be seen here in chrome dev tools.  It looks like it gets to 'yourmodule' but no further.
    http://imgur.com/cu4jtw6

Comment: are you using a drupal behavoir ? otherwise settings won't be populated
Comment: Per the docs you should not be using hook_init to add JS or CSS as it will not be fired on cached pages. Instead use your theme functions such as `theme_preprocess_html(&$vars)` [here](https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess_HOOK/7) are the docs.
Answer: your init hook seems to be wrong, change the function name to custombootstrap_init,
where the init hook is not the best choice for adding a js setting ..
in drupal it's good practice to use a behavoir, so wrap your js code like:
Drupal.behaviors.myModuleBehavoir = {
  attach: function (context, settings) {
    ..
  }
};

and then just use settings.yourmodule.myurl
Comment: when I change the function name to custombootstrap_init I get the same error,
Comment: try a different hook and make sure it gets hit

Not sure why I get Uncaught TypeError


PHP Database Entry Issue (Wampserver)

Question: I'm a complete code nooby and months ago built a basic website before my server mobo died. I just rebuilt the server and copied over all my website/db code I had backed up to my desktop. To the best of my knowledge everything is the same between php code and database setup.
My site works 90% except for when I go to input data. I get an error of "waiting for entry" opposed to the expected "New record created successfully". The relative if statement is on the very bottom of the below code.
Does anyone by chance have any clue why after inputting all my proper data that it does not trigger the "New record created successfully"? If it means anything the code works fine on my desktop and unless I did something really dumb I copy/pasta'd everything appropriately to this server :(
<code><?php

 //Connect to database

include('database.php');

   //grab user logged in for database insertion

   $user_check = $_SESSION['login_user'];

   $ses_sql = mysqli_query($dbcon,"select username from t_login where username = '$user_check' ");

   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

   $login_session = $row['username'];

 // Create DB Variable

 $login_session = $row['username'];
 $excercisename = (isset($_POST['excercisename']) ? $_POST['excercisename'] : null);
 $setcount = (isset($_POST['setcount']) ? $_POST['setcount'] : null);
 $rep1 = (isset($_POST['rep1']) ? $_POST['rep1'] : null);
 $rep2 = (isset($_POST['rep2']) ? $_POST['rep2'] : null);
 $rep3 = (isset($_POST['rep3']) ? $_POST['rep3'] : null);
 $rep4 = (isset($_POST['rep4']) ? $_POST['rep4'] : null);
 $rep5 = (isset($_POST['rep5']) ? $_POST['rep5'] : null);
 $rep6 = (isset($_POST['rep6']) ? $_POST['rep6'] : null);
 $rep7 = (isset($_POST['rep7']) ? $_POST['rep7'] : null);
 $rep8 = (isset($_POST['rep8']) ? $_POST['rep8'] : null);
 $weight1 = (isset($_POST['number1']) ? $_POST['number1'] : null);
 $weight2 = (isset($_POST['number2']) ? $_POST['number2'] : null);
 $weight3 = (isset($_POST['number3']) ? $_POST['number3'] : null);
 $weight4 = (isset($_POST['number4']) ? $_POST['number4'] : null);
 $weight5 = (isset($_POST['number5']) ? $_POST['number5'] : null);
 $weight6 = (isset($_POST['number6']) ? $_POST['number6'] : null);
 $weight7 = (isset($_POST['number7']) ? $_POST['number7'] : null);
 $weight8 = (isset($_POST['number8']) ? $_POST['number8'] : null);
 $cardio = (isset($_POST['cardio']) ? $_POST['cardio'] : null);
 $duration = (isset($_POST['duration']) ? $_POST['duration'] : null);
 $weight = (isset($_POST['weight']) ? $_POST['weight'] : null);
 $comments = (isset($_POST['comments']) ? $_POST['comments'] : null);

 //Execute the insert statement
 if(!empty($excercisename)){
 $sql = "INSERT INTO t_workout (username, excercisename, setcount, set1reps, set2reps, set3reps, set4reps, set5reps, set6reps, set7reps, set8reps, set1weight, set2weight, set3weight, set4weight, set5weight, set6weight, set7weight, set8weight, cardio, cardio_dur, weight, comments)
 VALUES ('$login_session', '$excercisename', '$setcount', '$rep1', '$rep2', '$rep3', '$rep4', '$rep5', '$rep6', '$rep7', '$rep8', '$weight1', '$weight2', '$weight3', '$weight4', '$weight5', '$weight6', '$weight7', '$weight8', '$cardio', '$duration', '$weight', '$comments')";
 }

  if($weight > 0){
     $sql = "INSERT INTO t_workout (username, weight)
     VALUES ('$login_session', '$weight')";
  }

if(isset($sql)){
    if ($dbcon->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Waiting for entry.";
}
}
$dbcon->close();
?>
</code>
Comment: If your weight is set then the $sql overwrites the first insert.  You probably want to run both inserts and not just the last one.
Comment: yea I remember that part. I forgot exactly why but on the site I specified that if you want to record your personal weight, leave everything else blank. But even then on my site entering only weight returns a "Waiting for entry" Only thing I can think of is that Wampserver on my desktop is 3.0.6, and on my server it is 3.1.0. Could that be the problem by chance? But I hesitate to think that because it's just code correct? and all the tables/php is there :(
Comment: I'm pretty lost. I uninstalled everything, installed the same Wampserver 3.0.6 on server. Placed in all my code from desktop for website/database over so that it should be identical. But still get the "Waiting for entry" error whereas that problem does not exist on desktop. Am very sad lol.
Comment: Can you add `<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);` at the top of the code to ensure errors are reported.
Answer: Sweet mercy I am sorry for anyone that looked into this lol.
I solved it. The code and everything is fine (well, for how amateur the code is).
The issue is with wampserver, in my mysql.ini file there was a semi-colon commenting out sql-mode. I simply removed the semicolon and now it works. I don't know what sql mode in the ini-file did...but its something I had edited in the ini file months ago that I caught when comparing all my wampserver config files.
;sql-mode=""

PHP Database Entry Issue (Wampserver)


Unable to generate rdlc report

Question: I am new to rdlc and I have a page which was developed by ex-employee of our company and I have to modify it by adding few more rdlc reports. So, I have copy paste the report and made the path dynamic. But I am getting error i.e.,

An error occurred during local report processing.

Here is my code.
if (oDataSet.Tables.Count > 0)
{
   oDataSet.Tables.Add(oDataTable);
   ReportDataSource rd = new ReportDataSource("Letter", oDataSet.Tables[0]);
   ReportDataSource rd1 = new ReportDataSource("DataSet1", oDataSet.Tables[1]);
   this.TemprptViewer.LocalReport.DataSources.Clear();
   this.TemprptViewer.LocalReport.DataSources.Add(rd);
   this.TemprptViewer.LocalReport.DataSources.Add(rd1);
   ReportParameter p2 = new ReportParameter("CurrentDate", DateTime.Today.ToString("MMMM dd, yyyy"));
   this.TemprptViewer.LocalReport.SetParameters(new ReportParameter[] { p2 });
   ReportParameter Reimbrs_payAnnual = new ReportParameter("Reimbrs_payAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["Reimbrs_payAnnual"].ToString()), 0).ToString());
   ReportParameter basicAnnual = new ReportParameter("basicAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["basicAnnual"].ToString()), 0).ToString());
   ReportParameter hraAnnual = new ReportParameter("hraAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["hraAnnual"].ToString()), 0).ToString());
   ReportParameter spl_allowAnnual = new ReportParameter("spl_allowAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["spl_allowAnnual"].ToString()), 0).ToString());
   ReportParameter tran_allowAnnual = new ReportParameter("tran_allowAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["tran_allowAnnual"].ToString()), 0).ToString());
   ReportParameter statutory_bonusAnnual = new ReportParameter("statutory_bonusAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["statutory_bonusAnnual"].ToString()), 0).ToString());
   ReportParameter fixed_grossAnnual = new ReportParameter("fixed_grossAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["fixed_grossAnnual"].ToString()), 0).ToString());
   ReportParameter Medi_ReimbursAnnual = new ReportParameter("Medi_ReimbursAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["Medi_ReimbursAnnual"].ToString()), 0).ToString());
   ReportParameter LTAAnnual = new ReportParameter("LTAAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["LTAAnnual"].ToString()), 0).ToString());
   ReportParameter Total_ReimburseAnnual = new ReportParameter("Total_ReimburseAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["Total_ReimburseAnnual"].ToString()), 0).ToString());
   ReportParameter pfAnnual = new ReportParameter("pfAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["pfAnnual"].ToString()), 0).ToString());
   ReportParameter gratuityAnnual = new ReportParameter("gratuityAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["gratuityAnnual"].ToString()), 0).ToString());
   ReportParameter total_retiral_benifitAnnual = new ReportParameter("total_retiral_benifitAnnual", Math.Round(Double.Parse(oDataSet.Tables[0].Rows[0]["total_retiral_benifitAnnual"].ToString()), 0).ToString());
   ReportParameter ctcWords = new ReportParameter("ctcWords", ctcWord);
   this.TemprptViewer.LocalReport.SetParameters(new ReportParameter[] { basicAnnual, hraAnnual, spl_allowAnnual, tran_allowAnnual, statutory_bonusAnnual, fixed_grossAnnual, Medi_ReimbursAnnual, LTAAnnual, Total_ReimburseAnnual, pfAnnual, gratuityAnnual, total_retiral_benifitAnnual, ctcWords, Reimbrs_payAnnual });
   page.Session["GridBind"] = "a";
   this.TemprptViewer.LocalReport.Refresh();
}

Error occurs in this line

An error occurred during local report processing

this.TemprptViewer.LocalReport.SetParameters(new ReportParameter[] { p2 });

Answer: Make sure that ReportViewer was not empty

fill the Report Data Source & Report Data Instance
ReportViewer
1- choose ReportViewer Task
2- Choose Data Source
3- fill Report Data Source
4- fill Report Data Instance

Unable to generate rdlc report


How to minimize the risk of employees spreading critical information?

Question: What's common sense when it comes to minimising the risk of employees spreading critical information to rivalling companies?
As of today, it's clear that not even the US government and military can be sure that their data stays safely within their doors. Thereby I understand that my question probably instead should be written as "What is common sense to make it harder for employees to spread business critical information?"
If anyone would want to spread information, they will find a way. That's the way life work and always has.
If we make the scenario a bit more realistic by narrowing our workforce by assuming we only have regular John Does onboard and not Linux-loving sysadmins , what should be good precautions to at least make it harder for the employees to send business-critical information to the competition?
As far as I can tell, there's a few obvious solutions that clearly has both pros and cons:

Block services such as Dropbox and similar, preventing anyone to send gigabytes of data through the wire.
Ensure that only files below a set size can be sent as email (?)
Setup VLANs between departments to make it harder for kleptomaniacs and curious people to snoop around.
Plug all removable media units - CD/DVD, Floppy drives and USB
Make sure that no configurations to hardware can be made (?)
Monitor network traffic for non-linear events (how?)

What is realistic to do in a real world? How does big companies handle this?
Sure, we can take the former employer to court and sue, but by then the damage has already been caused...
Thanks a lot
Comment: @mfinni that's a real "meta" kinda question but I dislike this kind of fragmentation of the sites I have to say.
Comment: @mfinni, my view is that this is a management issue, not system administration. Once policy has been formulated, preferably with the input of the sysadmins, it may well be up to the admins to implement it, which would then become an appropriate topic for SF. Formulation of management policy, which is what this question is about, is not SF material in my opinion).
Comment: As the question is asked, it's about how (technologically) to stop information leaking. Presumably, the policy has already been formulated.
Comment: It seems to me that this would be a far better fit on http://security.stackexchange.com/
Comment: @Zoretache I had no idea that that even existed. Feels like new Stackexchange sites appearing everyday :)
Comment: This could definitely be a sysadmin type of question - do we really want to shunt questions like this to more specialized sites?
Comment: @Zoredache I gotta get over there more often.
Comment: @robert-moir I agree.  I created a question on meta to discuss that issue.
Answer: There are a variety of things that can be done. The are entire industries created around the very idea of "how do I keep information from leaking". The ubiquity of static data-storage and wireless networks (both wifi and 3G/4G) make wired network-perimeter security less of the barrier than it was even 5 years ago.
As with all security, managing the exceptions can be very tricky. Yes, you can disable all USB ports, but that leaves USB keyboards, mice, and printers in the dark. You can disable all access to Facebook, but the Public Relations office will definitely need access. The extremely paranoid can ban all phones with cameras (lest someone phone-cam a doc and mail it to a competitor) but that's really hard to make stick these days. And then there is the old fashioned method of taking home printouts to fax.
If someone really wants to leak information, it's generally easy.
I can't stress enough the impact that municipal scale high-bandwidth networks have on security posture. With nearly everyone with a camera in their pocket and a phone-plan able to accommodate pictures, 1-5 page documents can be sent with ease without ever touching the corporate LAN. If USB connections are enabled, many smartphones can expose local storage to a workplace computer and have files saved on it which can then be sent from the phone directly if not sneakernetted home and sent from there.
The phone-cam 'attack' is particular insidious since it leaves no log-traces on company equipment the way that USB-mounts potentially can.
The ironic thing about Internet-access restrictions blocking social networking sites and all known webmail providers is that it forces people onto their phones for the same service.

Big companies handle this by ignoring the hard to manage threats (see above for a good example of one) and managing the risks they can mange cheaply. That means:

Blocking web-sites of any suspicious class (social media and webmail sites are big ban targets) and known web-proxy sites
Recording all outbound email
Enforce a captive-portal for internet access, requiring logon with corporate credentials before access is given
Monitoring outbound email for private data using varying complexities of filters (big industry with this one)
Ensuring least-privilege on the local network so people don't have access to secrets they don't need
Using asset-inventorying software to monitor corporate hardware for change events
Use event-log monitoring software to track hardware events such as use of removable media
Set Group Policies to ban certain behaviors deemed unneeded in the workplace
Using strong encryption on any WLANs in use

These days the network perimeter is not just at the WAN/LAN demarc, it touches every point of the network where data is released into analog form of any kind and the tools for exploiting such analog holes are getting ever better and ever more common. And other such things.
Answer: In the first case are we talking about malicious activity/corporate espionage? Or stupidity/negligence? (both are real issues).
In either case, start with making a note of what information sharing is needed to get the job done (remember the IT tail should never wag the business dog) and a workplace HR policy that spells out that this is all that is allowed and that infractions are disciplinary matters.
In addition to this, remember that employees are human - unless you're working somewhere that clearly and obviously requires draconian security polices then going over the top will alienate the staff and harm productivity - remember you're not securing the system because securing the system is good fun, you're doing it to protect the business. Part of protecting the business includes not annoying and disrupting employees for no good purpose.
Lastly, in terms of the "setting policy" points, remember that IT Security does not exist in a vacuum. There is little point in locking down the network if physical security is lax. Why would someone need to break into the network if they can just walk into an office and pick up the data in a nice little printout, hide it inside their lunchbox and walk out?
If you don't want people in one department talking about confidential business to people in another department then again, this needs to be made clear to them. Again, there is little point in locking down the network if someone can just buy a friend lunch and ask them about it.
Make sure printers and photocopiers are appropriately secured - people leave all kinds of documents sitting by the printer. And printers and photocopiers sometimes retain a copy of the last printed document in their memory...
As for the actual IT side of things...
Your 6 points are a good starting point but there's a few things to be aware of here:
Configuration lock-down and management is a good start, but how does information legitimately arrive at and leave the business? Can these workflows be abused?
If you're blocking up all the USB ports then how does the mouse work? These are usually USB these days after all. If you leave one port for that a determined thief could unplug the mouse and plug in a USB drive. So perhaps you need to consider software based blocking that prohibits the installation of certain classes of device.
You need to consider sophisticated email filtering if you want to go down that road. Rather than just assuming that any files over a certain size are to be blocked as bad, you need the sorts of email filtering that are available that scan content for keywords and patterns you define and take actions based on that.
Separating sensitive offices VLANs (if not totally physically separate networks) is good standard practice. Be aware that this will increase support costs and may be prone to breaches due to human error on the part of the support technicians who do the wiring.
One thing I would certainly do is ensure that data wasn't stored locally on workstations and laptops but rather saved onto central severs. These should be easier to secure both physically and electronically. Consider storing documents in document management systems that enable you to track access and changes made. This can also allow for IRM to control who can do what to a document. These systems aren't foolproof but can  help.
There's a lot more things you can say about this subject but these might give you some interesting thoughts. It's a subject that you can write a book about (indeed several people have).
Answer: The short answer is you can't and you have to be able to trust your users.  All you can do is limit what information someone has access to based on their trust level.  If they need it for their job, but you don't trust them to have it, then you have the wrong person in that position.  Most the critical data isn't going to be large but things like financial numbers, customer credit card numbers, and such.  For example, we only have credit card numbers go one way into the system and are never displayed to any user or even back to the customer.  Financial reports are audited and reviewed to see who is running them and from where.
Answer: As you mention, if somebody wants to get stuff out, they will find a way even if you have blocked the obvious routes.  For example, try to stop employees from using cell phone cams to do screengrabs or stealing printouts.
I prefer monitoring and mirroring of the obvious routes.  This encourages users to go the path of least (monitored) resistance.  Don't block all USB usage, instead log usage and mirror the data to a server (we use DriveLock for this) - it can also block USB storage without blocking USB keyboards and mice.  Set up auditing of the data periodically and look for odd transfers.
You can do similar stuff with BlueCoat appliances for the network, but you have to proxy all SSL connections to enable the device to audit all file transfers.  I'd look into these to cover your (1) and (6).  Users will be aware of the proxying though.
If you do completely block access to devices or sites, make sure to check the logs for failed attempts periodically to see who's attempting what.  Without intercepting the content itself though, attempts are easily deniable, e.g. by renaming CAD drawings to familyphoto.jpg.
For (5), use BIOS boot passwords to prevent unauthorized hardware changes or boot media, otherwise any user can boot with a LiveCD and copy whole drives.
(3) is silly as mentioned previously.  ACLs and content encryption is more appropriate for this.
Also, before auditing logs, have a plan as to what steps will be taken if more information needs to be gathered.  Who will ok more detailed sniffing if something illegal/actionable is found?  It's not uncommon to enable auditing in a medium size company and find something ugly right away.
Of course, talk to the appropriate legal contact in your organization before implementing any of these policies, and get permission in writing before doing anything.
Answer: The things you listed are decent, if you really have such critical info. From the technical side, log everything as well - you can't block EVERYTHING. Item 3 is kind of silly though - just set appropriate ACLs on shared resources (files, websites/SharePoint, etc).
From a non-technical perspective - you can have non-compete agreements. As you say, you can take an employee to court. This is also a deterrent, not just a remedy - remember that.
You can also run your business in such a way that your employees feel valued and would be less likely to engage in such actions. That may or may not be at odds with running the place like it's on lockdown and no one is to be trusted with any info.
What is the critical information that your company holds? If it's patented or a trade secret, then a competitor can't use it (legally, AFIAK, IANAL, etc.) If it's your employee's experience and skills, tough - you don't own that, unless it's someone so high-value that you really do have a binding contract with them for a certain period of time. If it's something proprietary (from my own experience, a large database of contractors that a service company trusted to do work nationwide) - well, that's tougher. Technically, don't expose the whole database; but someone who's committed to the project could collect the most important info over time and slowly abscond with it.


Grafana, Influxdb

Question: I have just installed influxdb. Am trying to create a data source but it is telling me this(photo). What should I do?enter image description here
Answer: Are you sure your InfluxDB is running?
It needs to be running in order to make a connection.

Grafana, Influxdb


Silverlight 4: Draw Vertical / Horizontal Rulers to Scale

Question: I am looking for options on how to draw 2 rulers at different scales on a canvas (assume a canvas) that will scale based on user-entered data.
Placing the tick marks and text one-time isn't a big deal, it is how to scale the data as the max/min values are changed by the user AND getting the points (ellipses) on the canvas to look correctly.
Foolishly, I set the size of the canvas to the max values of the current data, but as the data changes that won't work... I had hoped for a 1:1 translation...
Something like taking the current canvas size and redrawing the rulers is where I am headed
thanks,
rusty
Answer: I was over thinking the issue plus have just started in 'drawing', 'graphics', etc.  Have a lot to learn.  It was basic logical to physical translation math that most everyone 'knows'.
rusty


Angularjs Form/Field validation using JavaScript function without directives

Question: Is there a way to validate a field in angular without using a directive?
For example: I want to make following validation on an input field.

If field is empty we should show "Field must contain a value" message.
if field contains alpha Numeric characters we should show "Field can contain only digits".
An EVEN number - message to the user "Value must be an even number".

I want to make following validation in a call to JavaScript function.
I googled around and saw that there is a way to use ng-valid and $error , however I was not managed to make it work.
Code below is according to one of the answers I got:

<form name='theForm' novalidate>
    <input type='text' name='theText' ng-model='theText' ng-pattern='/^[0-9]+$/'/>
    Field can contain only digits
    <span ng-show='theText.length<1'>Field must contain a value
    Value must be an even number

<input type='submit' value='Submit' />
</form>

I want to take what inside the last [span] and put inside a JavaScript function in order to make it more generic and eventually change only JS and not the HTML when conditions are changing
Can someone please advise? a working example would be great.
Comment: If you wanna do all of these validations in a javascript function, why not go the angular way and create a custom validation directive?
Comment: Why do you want to use a Javascript function ? Angular can do it while watching the models like here : http://jsfiddle.net/DotDotDot/6UJZk/1/ , using another function wouldn't be as efficient I think
Answer: I'm surprised no one has mentioned ui-validate
$scope.isOdd = function($value){
  return $value % 2;
}
...
<form name="myform">
  <input ng-model="myVal" name="value" required
    ng-pattern="/^[0-9]*$/" ui-validate=" 'isOdd($value)' "></input>

{{myform.value.$error|json}}

</form>

Doesn't get any simpler than that, and it's PROPER AngularJS validation (not silly watches)
Here's a working demo
Comment: this is exactly what I was looking for, thanks.
Can you please add a working example? **I could not make your example work**
Comment: @Scription added. Looks like the stupid thing requires a *form* to work. Don't forgot the `ui.validate` module
Answer: Take a look at the angularjs form documentation - http://docs.angularjs.org/guide/forms . In general, it is based on the HTML5 attributes like required, min, max, etc.
To get, for example, your first requirement done - "an empty field should show "Field must contain a value" message, yo uwould do something like that:
<input type="text" ng-model="user.name" name="uName" required />

  Field must contain a value.

For digits only field you can use the pattern attribute with a matching regular expression (example: http://www.wufoo.com/html5/attributes/10-pattern.html).
For even number validation, I'm not sure - I think you'd have to go with custom validation for that (meaning you'd have to create a directive) or use the pattern attribute somehow.
Last but not least - remember to add novalidate to the <form> tag. Otherwise the browser will try to validate your fields as well and you don't want that:
<form ... novalidate>
 ...
</form>

Comment: So why are you using angularjs if you don't wanna use its capabilities?
Comment: You answered my q only regarding "require" attribute.
As for the rest, maybe I did not make my Q clear enough, I want to use JavaScript function that would make the correct validation on each user key press to the field (or leaving the field)
Comment: I wrote what should be done for the digits and even number validation - for digits you can use the pattern attribute (no need for custom JS function). For the even number validation, that's trickier and I don't think you will be able to get away without a directive.
Comment: I don't want to use directive at all  ONLY JS
Answer: I know the question is old and I know you didn't want a directive but you may consider using a directive if it's "Angular" way... Well here is my Angular-Validation. I made a project on Github and I think that it just rocks compare to whatever is/was available...I based myself on the excellent Laravel PHP Framework and made it available under Angular... It is so crazy simple, you need 2 lines 1 line of code, 1 line for the input, 1 line for error display, that's it... never more and never less!!! Enough said, let's give some examples:

Email
<input type="text" validation="email|min_len:3|max_len:25|required" ng-model="form1.input1" name="input1" />

Alphanumeric + Exact(3) + required
<input type="text" validation="alpha|exact_len:3|required" ng-model="form1.input2" name="input2" />

So I can define whatever amount of validation rules (already 25+ type of validators) which I want in a simple directive validation="min_len:2|max_len:10|required|integer" and the error message will always display in the next  Don't you guys like it already? 1 line of code for your input, 1 line of code for the error display, you can't be simpler than that...oh and I even support your custom Regex if you want to add. Another bonus, I also support whichever trigger event you want, most common are probably onblur and onkeyup. Oh and I also support multiple localization languages via JSON external files. I really added all the imaginable features I wanted into 1 crazy simple directive.
No more clustered Form with 10 lines of code for 1 input (sorry but always found that a little extreme) when the only thing you need is 2 lines, no more, even for an input with 5 validators on it. And no worries about the form not becoming invalid, I took care of that as well, it's all handled the good "Angular" way.
Take a look at my Github project Angular-Validation... I'm sure you'll love it =)
UPDATE
Another candy bonus! To make an even more smoother user experience, I added validation on timer. The concept is simple, don't bother the user while he's typing but do validate if he makes a pause or change input (onBlur)... Love it!!!
You can even customize the timer as per your liking, I've decided to default it to 1 second within the directive but if you want to customize you can call as for example typing-limit="5000" to make a 5 sec. timeout. Full example:
<input type="text" ng-model="form1.input1" typing-limit="5000" validation="integer|required" name="input1" />

UPDATE #2
Also added input match confirmation validation (ex.: password confirmation), here is a sample code

Password
<input type="password" name="input4" ng-model="form1.input4" validation="alpha|min_len:4|required"  />
Password Confirmation
<input type="password" name="input4c" ng-model="form1.input4c" validation="match:form1.input4,Password|required"  />

UPDATE #3
Refactored the directive so that the requirement of having a  to display the error is unnecessary, the directive now handles it by itself, see the code change reflected on top.
DEMO
Added a live demo on Plunker
Comment: @ProLoser.. Because it's crazy simple, your code get shorter and you don't even need to write error messages as the directive does it for you. And now I just finished a candy bonus, to have smoother user experience I only validate after user inactivity. Love it =)
Comment: Thanks for the effort and plug-in
Comment: You're welcome, hope lot of people would use it and help me improve it... please note that I corrected a bug today which is maybe after you tried it, make sure you use latest code...Thanks... and I'm curious, would you use it? Do you like the concept?
Comment: @ghiscoding I'm curious, why do you feel this approach is better than using ``? I think all the use-cases your project demos could be covered by these 3 attributes.
Answer: Well you can try to create a func
Value must be an even number

$scope.isEven=function(data) {
    if(data) {
        return data%2===0
    }
    return true;
}

The method can either be defined on the current controller scope or on $rootScope.
Not a very angular way, as directives would be better but i think it would work.
Comment: I want to use JavaScript and be able to change it instead of changing the HTML. however, if directive is the "Angular" way then I'm willing to consider it as well.

**Can you please give me an example how to use it in directive?**
Comment: If you look at the angular developer guide for forms, and check for `custom validation` section you would see a working example there http://docs.angularjs.org/guide/forms


Potential use for data that contains its own hash?

Question: I was writing some code to simulate the proof-of-work done by the Bitcoin network while generating blocks when I suddenly became curious: How can you create a datum that contains it's own hash?
Just for fun, I wrote a program that attempts to create data that contains it's own hash. 4 random bytes are generated, then a nonce is added at the end and the whole value is hashed with CRC32. The nonce in incremented and the process repeats until the program finds a hash that matches the original 4 bytes.
Note: The nonce can increase indefinitely.
Here's an example of the output after approx 1,980,000,000 attempts:
Found a match!
Data: 7a73a2d4ab833876
Original hash: 7a73a2d4 new hash: 7a73a2d4

Is there a potential use for this?
package selfhash;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

/**
 *
 * @author dylan
 */
public class SelfHash {

    static byte[] data;
    static byte[] hash = new byte[4];

    public static void main(String[] args) {
    // TODO code application logic here
    SecureRandom random = new SecureRandom();
    random.nextBytes(hash);
    data = new byte[hash.length + 1];
    System.arraycopy(hash, 0, data, 0, hash.length);
    long c = 0;
    while (true) {
        recalculateData();
        byte[] dataHash = crc32AsByteArray(data);
        if (c % 10000000 == 0) {
        System.out.println("Calculated " + c + " hashes");
        System.out.println("Data: " + byteArrayToHex(data));
        System.out.println("Original hash: " + byteArrayToHex(hash) + " new hash: " + byteArrayToHex(dataHash));

        }
        if (Arrays.equals(hash, dataHash)) {
        System.out.println("Found a match!");
        System.out.println("Data: " + byteArrayToHex(data));
        System.out.println("Original hash: " + byteArrayToHex(hash) + " new hash: " + byteArrayToHex(dataHash));
        break;
        }
        c++;
    }
    }

    public static void recalculateData() {
    int position = hash.length;

    while (true) {
        int valueAtPosition = unsignedToBytes(data[position]);
        if (valueAtPosition == 255) {
        //increase size of data
        if (position == data.length-1) {
            byte[] newData = new byte[data.length + 1];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
        }
        data[position] = (byte) (0);
        position++;
        } else {
        data[position] = (byte) (valueAtPosition + 1);
        break;
        }
    }
    }

    public static byte[] hexToByteArray(String hexString) {
    int len = hexString.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
            + Character.digit(hexString.charAt(i + 1), 16));
    }
    return data;
    }

    private static final char[] BYTE2HEX = ("000102030405060708090A0B0C0D0E0F"
        + "101112131415161718191A1B1C1D1E1F"
        + "202122232425262728292A2B2C2D2E2F"
        + "303132333435363738393A3B3C3D3E3F"
        + "404142434445464748494A4B4C4D4E4F"
        + "505152535455565758595A5B5C5D5E5F"
        + "606162636465666768696A6B6C6D6E6F"
        + "707172737475767778797A7B7C7D7E7F"
        + "808182838485868788898A8B8C8D8E8F"
        + "909192939495969798999A9B9C9D9E9F"
        + "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
        + "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
        + "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
        + "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
        + "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
        + "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF").toLowerCase().toCharArray();

    ;

  public static String byteArrayToHex(byte[] bytes) {
    final int len = bytes.length;
    final char[] chars = new char[len << 1];
    int hexIndex;
    int idx = 0;
    int ofs = 0;
    while (ofs < len) {
        hexIndex = (bytes[ofs++] & 0xFF) << 1;
        chars[idx++] = BYTE2HEX[hexIndex++];
        chars[idx++] = BYTE2HEX[hexIndex];
    }
    return new String(chars);
    }

    public static String sha256AsHexString(byte[] bytes) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return byteArrayToHex(digest.digest(bytes));
    } catch (Exception e) {
        throw new Error(e);
    }
    }

    public static byte[] sha256AsByteArray(byte[] bytes) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return digest.digest(bytes);
    } catch (Exception e) {
        throw new Error(e);
    }
    }

    public static byte[] crc32AsByteArray(byte[] bytes) {
    Checksum checksum = new CRC32();
    checksum.update(bytes, 0, bytes.length);
    long value = checksum.getValue();
    byte[] resultExcess = ByteBuffer.allocate(8).putLong(value).array();
    byte[] result = new byte[4];
    System.arraycopy(resultExcess, 4, result, 0, 4);
    return result;
    }

    public static int unsignedToBytes(byte b) {
    return b & 0xFF;
    }
}

Answer: I can't think of a use.
CRC's are linear, and so the equations can be solved very fast to get the second four bytes. You don't need two billion trials to find it. See spoof.c.
Comment: What about a different hash function, like sha256? Do you think this could be used as a proof-of-work for cryptocurrency?
Comment: If you could do this with SHA-256, then SHA-256 would need to be replaced. The whole point of SHA-256 is to make this impossible with existing or foreseeable computers. Compute 2^256 to see how many trials you would need.
Comment: I rewrote it to use the first 4 bytes of sha256 instead of crc32. It created 6 solutions after running all night. I understand it would pretty much be impossible with the full 32 bytes.


Bijective polynomials $f\in\mathbb Q[X_1,\dots,X_n]$

Question: Does it exist a polynomial $f\in\mathbb Q[X_1,\dots,X_n]$, of degree greater than $1$, that defines a one-to-one correspondence
$f:\mathbb Z^n\to\mathbb Z$ or $f:\mathbb N^n\to\mathbb N$?
Are there methods to approach such questions at all?

See also Conjecture about polynomials $f_n\in\mathbb Q[X_1,\dots,X_n]$ defining bijections $\mathbb N^n\to\mathbb N$
Answer: It is known that the polynomial $f(n,m)=\frac{1}{2}(n+m)(n+m+1)+m$ defines bijection
$\mathbb{N}\times\mathbb{N}\to\mathbb{N}$.
Reference: Polynomial bijections.


Installation of apache on aws throwing an error

Question: When i try and run the command
yum install -y httpd.x86_64

it throws the following error
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
No package httpd.86_64 available.
Error: Nothing to do

What should i do to troubleshoot?
Comment: Looks like a typo to me. Try again.
Answer: Try installing the standard httpd package
sudo yum update
sudo yum install httpd


Login user programatically (Create user session)

Question: My question is based on this question: Programmatically User login not working i dont have this clear, to login an user (create an active session) only need use User::Load()? i'm triying make this on my custom module, due to i need an independent login and register form, but dont work. Someone can help me? i want create a user session and let the user navigate for all sections that your role have assigned, but without pass by "user/login" route. Thanks in advance. On resume i want emulate the login controller shipped with drupal 8.
Comment: Possible duplicate of [Programmatically User login not working](https://drupal.stackexchange.com/questions/250597/programmatically-user-login-not-working)
Comment: User::load() loads information about an account, it does not inject or alter the current session in any way. `user_login_finalize()` is one of the key functions.
Comment: Ok, how i can create a session for an user programatically? can you help me?
Comment: Ohh!!! ok, i get it, user_login_finalize() open a session for the provided user. Thanksa a lot!!! :D. Please put the comment in an answer for select as the correct answer.
Comment: Is the same article that i refer on my question, but for novices as me, is not obvious the response in the related topic, due to acepted response says: Delete user_login_finalize($user); and this line of code is just the line that log an user.
Answer: User::load() loads information about an account, it does not inject or alter the current session in any way. user_login_finalize() is one of the key functions when doing programatic user logins because it will create and set a session for the current user.
See more:
https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/user_login_finalize/8.7.x
Comment: Sorry, forgot to link to it. Updated.


Webservice Php sem Lib

Question: Opa, dei uma olhada no Stack atrás de uma questão simples, porém que está me deixando confuso.
Conhecer o conceito de Webservice é relativamente fácil, porém a prática é o foco desta questão. Vi algumas outras perguntas por aqui, mas que não foram respondidas devido a como a pergunta foi colocada, então pretendo especificar, exemplificar o melhor possível para que possam me ajudar.
É fácil encontrar Libs para se desenvolver um Webservice, a questão é, conceitualmente posso criar um Webservice sem nenhuma Lib? Por exemplo, receber o dado via Post, ou Get, manipula-lo e retornar uma resposta em Json, ou XML para uma aplicação desenvolvida em uma plataforma diferente? Mantendo interoperabilidade e outras características de um webservice...
Como Gambiarra já fiz isso:
<?php
    include 'servidor.php';

    $resposta=array();

    if(isset($_GET['comando'])){
        $comando=$_GET['comando'];
        switch($comando){
            case "Inserir":
                if(isset($_GET['nome']) && isset($_GET['ra'])){
                    $r=inserirContato($_GET['nome'] , $_GET['ra']);
                        if($r){
                            $resposta['codigo']=1;
                            $resposta['mensagem']="Sucesso ao inserir!";
                        }else{
                            $resposta['codigo']=0;
                            $resposta['mensagem']="Erro ao inserir!";
                        }
                }else{
                    $resposta['codigo']=0;
                    $resposta['mensagem']="Campos ausentes!";
                }
        break;

Mas também já fiz isso:
<?php
    include 'conexao.class.php';
    include 'lib/nusoap.php'; // BAIXADO DA INTERNET

    $server = new nusoap_server;
    $server->configureWSDL('agenda','urn:agenda');
    $server->wsdl->schemaTargetNamespace='urn:agenda';

    $server->wsdl->addComplexType(
        'Contato',
        'complexType',
        'struct',
        'all',
        '',
        array(
            'id'=>array('name'=>'id','type'=>'xsd:int'),
            'nome'=>array('name'=>'nome','type'=>'xsd:string'),
            'telefone'=>
            array('name'=>'telefone','type'=>'xsd:string')
        )
    );

    $server->wsdl->addComplexType(
        'ArrayString',
        'complexType',
        'array',
        '',
        'SOAP-ENC:Array',
        array(),
        array(
            array('ref'=>'SOAP-ENC:arrayType',
            'wsdl:arrayType'=>'xsd:string[]')
        ),
        'xsd:string'
    );

    $server->register(
        'procurarContato',
        array('id'=>'xsd:int'),
        array('return'=>'tns:Contato'),
        'urn:agenda',
        'urn:agenda#procurarContato',
        'rpc',
        'encoded',
        'Procurar contato'
    );

    function procurarContato($id){
        $con = new conexao();
        if($con->connect()){
            $query=mysql_query("SELECT * FROM contato where id='$id'");
            $row = mysql_fetch_array($query);
            $contatos=array(
                'id'=>$row['id'],
                'nome'=>$row['nome'],
                'telefone'=>$row['telefone']
            );
            $con->disconnect();
            return $contatos;
        }else return null;
    }

Em relação a entrega da informação desejada ambos funcionaram, embora um eu usei uma lib pra ter uma documentação e ser algo mais formal e a outra não. Pode parecer que a minha pergunta é a resposta dela mesmo, porém ai que está a minha dúvida, essas duas formas acima são Webservices? Se não, como classificaria a primeira forma? Posso melhorar a questão se parecer ampla ou vaga demais. (Lembrando que os trechos dos códigos colados aqui são apenas para ilustrar o uso da biblioteca, ainda há as funções, os parses para as responses etc.
Comment: `conceitualmente posso criar um Webservice sem nenhuma Lib?` Conceitualmente, as Libs são webservices "padrão", criados sem nenhuma lib, pra você não ter que criar a sua :)
Comment: show de bola Ricardo, muito obrigado!
Answer: A grosso modo, ambos podem ser considerados webservices sim.
O que muda entre eles é o padrão a ser utilizado.
No primeiro caso não existe padrão nenhum, só uma mensagem de confirmação e nada mais. O tratamento de erros não é intuitivo, entre outras coisas.
Já o segundo caso você está utilizando um padrão chamado SOAP. Esse padrão utiliza um formato específico de XML para definir o tipo de requisição esperada e qual a resposta deve ser retornada, de acordo com a implementação do seu sistema. O que muda é que o tratamento de erros, o formato das respostas seguem uma especificação padrão.
Como você irá implementar isso que é o ponto. Você pode tentar criar um webservice para comunicação entre sistemas SOAP sem nenhuma lib, mas não será muito produtivo como utilizar uma lib pronta. Seguir toda uma especificação não é algo trivial.
Ao fornecer um webservice, pense não na sua implementação, mas sim em como a pessoa que vai consumir irá trabalhar. Ao usar um padrão já conhecido, a pessoa que for consumir o serviço irá se focar no que ela precisa e não em ficar decifrando sua API mágica.
O que você prefere como usuário, consumir o serviço do exemplo 1 ou o serviço do exemplo 2?
Comment: Entendi perfeitamente, com certeza o exemplo 2 porque possuiria uma documentação e um padrão mais consistente para seguir. Muito obrigado, brother!

Webservice Php sem Lib


Best way to exclude the if's, which in result calls multiple methods

Question: I have a form made in JSP, here I have multiple buttons - "Approve, Reject, Save, Cancel".
For every submit, the control goes to a single Controller(Servlet), and there I am handling this submit as ::
String methodName = (String) request.getParameter("methodName");
if(methodName.trim.toLower().equals("approve"))
{
  approve_Claim(parameters);
}
else if(methodName.trim.toLower().equals("reject"))
{
  reject_Claim(parameters);
}
else if(methodName.trim.toLower().equals("save"))
{
  save_Claim(parameters);
}
else if(methodName.trim.toLower().equals("cancel"))
{
  cancel_Claim(parameters);
}

Is there a way to remove these multiple if's,
please suggest
Answer: It looks like you basically want a mapping from methodName.trim().toLower() to some sort of "claim action" which takes parameters. Create an interface like this:
interface ClaimAction
{
    void execute(Parameters parameters);
}

Then implement this with classes of CancelClaimAction, ApproveClaimAction etc. Next create a Map<String, ClaimAction> mapping "approve" to an ApproveClaimAction etc. Then:
String actionName = methodName.trim().toLower(Locale.US));
ClaimAction action = CLAIM_ACTION_MAPPING.get(actionName);
if (action != null)
{
    action.execute(parameters);
}
else
{
    // Error case, presumably.
}

You could use enums to do this, but I'd expect each of these classes to have enough work to do that it's worth separating them out and testing each one individually.
Comment: just curious to know:  this looks some how similar to Factory pattern to me. Can we state same with factory pattern?
Comment: @HarryJoy: It's more like the strategy pattern than the factory pattern. We're not really *creating* anything, so I don't see how it's factory-like. Unless you're thinking that the map itself is the factory, which is a bit of a stretch IMO.
Comment: You mean this: [Strategy_pattern](http://en.wikipedia.org/wiki/Strategy_pattern), right? Didn't know about it, thought me something new, Thanks.
Comment: @HarryJoy: Yes, that's what I meant. Whether it's an entirely appropriate name for what's going on here is a different matter, as the purpose of each action is different. I try not to get too hung up on patterns though to be honest - I try to use them as a starting place for ideas, rather than trying to analyze all the code I write and put it in buckets. They were great when first written up, but I think they've been over-emphasized a bit.
Answer: Some alternatives coming in my minds are with switch statement:

Pass integers instead of strings in parameter, parse them to int in servlet and then use switch case on this integers.
If you are using Java 7 then use switch case over string.
In java 6 take Enum of this strings and then switch over them - for older versions, upto java 5, (as Enum are introduced in java 5, mentioned by Jon skeet)
Comment: Enums were introduced in Java 5, not Java 6.
Comment: @JonSkeet I'm just saying that if he is not using java 7 and using java 6. Cause most of out there uses Java 6.
Comment: But why limit it artificially? Why explicitly state "Java 6" with the *implication* that if you're using Java before 6, you can't use enums?
Answer: The comparison about wich action was triggered has to be done somewhere. You can remove this logic from this place by using maybe something like this
public void handleRequest(HttpServletRequest request, String action){}

but as i said , the validation has to come somewhere in your code....
Answer: Well first suggestion will be that you can re-think on the  approach itself. Lots of MVC frameworks are out there (like struts, struts2, spring MVC, play framework) which does all the handling part for you keeping you actual code very simple and clean.

Assuming you have some specific purpose to use your own implementation I would suggest you can use java reflection for this.
String methodName = (String) request.getParameter("methodName");
this.getClass().getMethod(methodName).invoke(this);

What you need to do here, is keep the methodNames same as the acutal java methods.
To pass the parameters, you can do it this way -
String methodName = (String) request.getParameter("methodName");
this.getClass().getMethod(methodName, Paramter.class).invoke(this, parameters);


Simplify $\sin (90 - \theta)$

Question: Title.
I have no idea what to do. Is their an identity I have to remember? What am I supposed to do to the equation? Do I have to solve for something first, what does it mean by simplify?
Comment: sketch a right triangle and write the sin and cos ratios for both acute angles
Comment: ever heard of $sin(A-B)$ equality, where A can be 90 and B is $\theta$?
Comment: Hint: Draw both $\sin(\theta)$ and $\sin(90-\theta)$ and see how you can relate them.
Comment: @Sid I am not familiar with that or how to utilize it..
Comment: Hint: Prove that $\sin(90^\circ - \theta) = \cos(\theta)$ by drawing out a right-angled triangle with hypotenus of length $1$.
Answer: $\theta$ and $90°-\theta$ are called complementary angles. (Similarly, $\theta$ and $180°-\theta$ are called supplementary angles.)
Among the very first trigonometric identities that you must know by heart, you find
Opposite:
$$\sin(-\theta)=-\sin\theta\text{, }\cos(-\theta)=\cos\theta.$$
Complementary:
$$\sin(90°-\theta)=\cos\theta\text{, }\cos(90°-\theta)=\sin\theta.$$
Supplementary:
$$\sin(180°-\theta)=\sin\theta\text{, }\cos(180°-\theta)=-\cos\theta.$$
Comment: As an exercise, you can find by yourself the formulas for anticomplementary angles ($\theta, 90°+\theta$) and antisupplementary ones ($\theta, 180°+\theta$).
Comment: This was very helpful. Thank you for your time.
Answer: $\sin(\alpha-\beta)=\sin(\alpha)\cos(\beta)-\cos(\alpha)\sin(\beta)$
with
$\alpha = 90$
you get
$\sin(90) = 1$ and $\cos(90) = 0$
So it becomes
$1*\cos(\theta) - 0*\sin(\theta) = \cos(\theta)$
Answer: $\sin(90-\theta)=\cos(\theta)$ because:
$\sin(\alpha-\beta)=\sin(\alpha)\cos(\beta)-\cos(\alpha)\sin(\beta)$
Comment: How is sin(a-b) equal to what you elaborated upon? wouldn't it be sin a * sin - b ? Were did cos come from??
Comment: @sasha: it's an usual trigonometric formula. See for example: http://en.wikipedia.org/wiki/Proofs_of_trigonometric_identities
Comment: @sasha this is not literal multiplication, its called function notation, I hope you know that by now

Simplify $\sin (90 - \theta)$


Dotnet : Dockerfile No such file or directory

Question: I don't understand what's exactly the issue is. I am trying to dockerize my orchard web app. But I always have this issue of no such file or directory, but when I try to ls into this directory i can clearly see my .csproj
Here is my dockerfile:
FROM  mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
RUN mkdir p FGIC.WebCMS
COPY FGIC.WebCMS/FGIC.WebCMS.csproj FGIC.WebCMS/.
RUN dotnet restore FGIC.WebCMS/FGIC.WebCMS.csproj
COPY . .
RUN ls -al /src
RUN ls -al /src/FGIC.WebCMS
WORKDIR /src/FGIC.WebCMS
RUN dotnet build FGIC.WebCMS.csproj -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "/src/." -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "FGIC.WebCMS.dll"]

This is the error that appear in the layer of building the docker image
  WebCompiler: Begin compiling compilerconfig.json
  WebCompiler installing updated versions of the compilers...
 /root/.nuget/packages/buildwebcompiler/1.12.394/build/BuildWebCompiler.targets(12,9): error : No such file or directory [/src/FGIC.WebCMS/FGIC.WebCMS.csproj]

Build FAILED.

/root/.nuget/packages/buildwebcompiler/1.12.394/build     /BuildWebCompiler.targets(12,9): error : No such file or directory [/src/FGIC.WebCMS/FGIC.WebCMS.csproj]
0 Warning(s)
1 Error(s)

Time Elapsed 00:00:06.37
The command '/bin/sh -c dotnet build FGIC.WebCMS.csproj -c Release -o /app/build' returned a non-zero code: 1

And this is the structure of my app folder:
|-- Dockerfile
|--FGIC.sln
|--FGIC.WebCMS/ --|FGIC.WebCMS/FGIC.WebCMS.csproj
                --| other files of the project

Comment: can you check on the file permission ?
Comment: this is the permissions of my csproj file : -rw-rw-r--
Comment: @SKos I gived all permissions but it shows me the same error
Answer: It was a problem in the csproj file. It contains this line:
    <PackageReference Include="BuildWebCompiler" Version="1.12.394" />

So after deleting this line, everything works right.
Answer: Can you try this in the second part, please ? Seems like a directory problem.
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY . .
RUN dotnet restore FGIC.WebCMS/FGIC.WebCMS.csproj
RUN ls -al /FGIC.WebCMS
RUN dotnet build FGIC.WebCMS/FGIC.WebCMS.csproj -c Release -o /app/build

Comment: I tried this one but it fails with the same error


Can I host Angular2 frontend and Golang backend in one server

Question: I want to create RESTful API with Golang and frontend with Angular2.
Communication will be made with http requests. Angular2 will send requests to Golang API's. I know for Angular2 I should run own http server for routing and services.
Can I run Golang server on one host and Angular2 server on another one and connect them together?
Answer: Angular2 applications correspond to a set of static files (dependencies and application code). To have your application served by Go, you need to add some code to serve these files.
It seems possible. See this link:

https://github.com/golang/go/wiki/HttpStaticFiles

Edit
Following your comment:

If you want to host Angular2 and golang in one server. For example i will have access to web site with link mywebsite.com and access to golang api api.mywebsite.com

I can't see any reason not to do that. Just be careful to support CORS in your API (send the CORS headers in the response and support prefligthed requests). See these links:

http://restlet.com/blog/2015/12/15/understanding-and-using-cors
http://restlet.com/blog/2016/09/27/how-to-fix-cors-problems/
Comment: One can even include those assets into the binary, using things like [bindata-assetfs](https://github.com/elazarl/go-bindata-assetfs) or [go.rice](https://github.com/GeertJohan/go.rice).
Comment: I want to use golang and angular2 separatly. Golang server will send only json data. Angular will request data from golang server api's and render web pages. Can i host  angular and golang in one server? For example  i will have access to web site with link http://mywebsite.com and access to golang api http://api.mywebsite.com
Comment: I can't see any reason not to do that. Just be careful to support CORS in your API. See http://restlet.com/blog/2015/12/15/understanding-and-using-cors/
Answer: Actual implementation with the stadand library
<code>type Adapter func(http.Handler) http.Handler

// Adapt function to enable middlewares on the standard library
func Adapt(h http.Handler, adapters ...Adapter) http.Handler {
    for _, adapter := range adapters {
        h = adapter(h)
    }
    return h
}

// Creates a new serve mux
mux := http.NewServeMux()

// Create room for static files serving
mux.Handle("/node_modules/", http.StripPrefix("/node_modules", http.FileServer(http.Dir("./node_modules"))))
mux.Handle("/html/", http.StripPrefix("/html", http.FileServer(http.Dir("./html"))))
mux.Handle("/js/", http.StripPrefix("/js", http.FileServer(http.Dir("./js"))))
mux.Handle("/ts/", http.StripPrefix("/ts", http.FileServer(http.Dir("./ts"))))
mux.Handle("/css/", http.StripPrefix("/css", http.FileServer(http.Dir("./css"))))

// Do your api stuff**
mux.Handle("/api/register", Adapt(api.RegisterHandler(mux),
    api.GetMongoConnection(),
    api.CheckEmptyUserForm(),
    api.EncodeUserJson(),
    api.ExpectBody(),
    api.ExpectPOST(),

))
mux.HandleFunc("/api/login", api.Login)
mux.HandleFunc("/api/authenticate", api.Authenticate)

// Any other request, we should render our SPA's only html file,
// Allowing angular to do the routing on anything else other then the api
// and the files it needs for itself to work.
// Order here is critical. This html should contain the base tag like
// <base href="/"> *href here should match the HandleFunc path below
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "html/index.html")
})
</code>


Determinar os índices de um vetor que devem ser subtraídos

Question: Possuo o seguinte vetor:
a = [10, 20, 30, 40, 50, 60...]

Preciso criar uma nova lista que seja a subtração do índice posterior pelo anterior. Por exemplo:
b = [indice[0] - indice[1], indice[2] - indice[3], indice[4] - indice[5]...]

Neste caso, preciso ter como resposta os valores:
b = [10, 10, 10...]

Alguém poderia me ajudar dizendo como faço isso?
Grato pela ajuda.
Answer: Veja funcionando aqui.
a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
b = []

# i = indice anterior
i = 0
# p = proximo indice
p = 1
# Divide o tamanho total por 2
total = len(a) / 2
for x in xrange(0, len(a)):
  if x < total:
    b.append((a[p] - a[i]))
    i += 2
    p += 2

print(b)

Comment: Muito Obrigado!
Answer: Segue um exemplo:
a = [10, 20, 50, 80, 50, 60];
b = [];

for i in range(len(a)):
  if (i+1) < len(a):
    b.append(a[i+1]-a[i]);
    del(a[i])

print (b);

Comment: Muito Obrigado!
Answer: Seja a um array. Utilizando list comprehension, fazemos:
a = [10, 20, 50, 80, 50, 60]
b = [a[i+1]-a[i] for i in range(0,len(a),2) if i+1 < len(a) ]
print b

Desta forma, estamos apenas iterando os elementos dois a dois.
Comment: Muito Obrigado!


Gatsby: getImage returns undefined

Question: getImage is returning undefined so my GatsbyImage component is not rendered.
File structure:

src/pages/gallery.js
src/images (has 12 photos named photo-01.jpg, photo-02.jpg, ...)

I have the following code (gallery.js):
import React from "react";
import Layout from "../components/Layout";
import { useStaticQuery, graphql } from "gatsby";
import { GatsbyImage, getImage } from "gatsby-plugin-image";

const Gallery = () => {
  const { gallery } = useStaticQuery(graphql`
    query {
      gallery: allFile(
        filter: {
          extension: { eq: "jpg" }
          absolutePath: { regex: "/images/" }
        }
      ) {
        nodes {
          id
          childImageSharp {
            fluid(maxWidth: 500, maxHeight: 500) {
              ...GatsbyImageSharpFluid_tracedSVG
            }
          }
        }
      }
    }
  `);

  return (
    <Layout>

Gallery

          {gallery.nodes.map((image) => (

              <GatsbyImage
                image={getImage(image.childImageSharp.fluid)}
                alt="Gallery"
              />

          ))}

    </Layout>
  );
};

export default Gallery;

what can i have wrong?
Answer: The problem is that you are mixing gatsby-image (from Gatsby 1 to Gatsby 2) and gatsby-plugin-image (from Gatsby 3 onwards). The first one is now deprecated.
This can be easily spotted when you query for fluid or fixed GraphQL nodes since they are created by gatsby-image which uses Img (from 'gatsby-image') component, which accepts a fluid or fixed property.
On the other hand, gatsby-plugin-image queries for gatsbyImageData (not fluid or fixed) and the corresponding GatsbyImage (from 'gatsby-plugin-image') component accepts an image property.
In your case, you are querying a gatsby-image structure applied in a gatsby-plugin-image component, that's why it is returning undefined.
Check the migration guide but essentially you will need to replace (and remove) all references to gatsby-image and install the required dependencies:
npm install gatsby-plugin-image gatsby-plugin-sharp gatsby-transformer-sharp

Add it to your gatsby-config.js:
module.exports = {
  plugins: [
    `gatsby-plugin-image`,
    `gatsby-plugin-sharp`,
    `gatsby-transformer-sharp`,
  ],
}

And change your query to something like:
const Gallery = () => {
  const { gallery } = useStaticQuery(graphql`
    query {
      gallery: allFile(
        filter: {
          extension: { eq: "jpg" }
          absolutePath: { regex: "/images/" }
        }
      ) {
        nodes {
          id
          childImageSharp {
            gatsbyImageData(layout: FIXED)
          }
        }
      }
    }
  `);

  return (
    <Layout>

Gallery

          {gallery.nodes.map((image) => (

              <GatsbyImage
                image={getImage(image.childImageSharp)}
                alt="Gallery"
              />

          ))}

    </Layout>
  );
};

export default Gallery;

Double-check the query since the nodes and the structure may be different when applying gatsby-plugin-image.
Note that getImage is a helper function and you may not need it, consider using directly:
  <GatsbyImage
    image={image.childImageSharp.gatsbyImageData}
    alt="Gallery"
  />

Comment: Thanks Ferran Buireu! I found the Gastby documentation a bit confusing, but you explained it to me very clearly.

Gatsby: getImage returns undefined


How does the behavior of a function change if it is within a subshell?

Question: When making functions within a script it bash, it appears that people often have the function run within a subshell, ie
function(){(

)}

instead of
function(){

}

What are the benefit/downsides of using {()} rather than just {} if any?
Comment: Aside: you can use `function() (...)` directly
Answer: Parentheses cause the function to run in a subshell, which is a child process isolated from the parent shell. They're useful when you want to make process-wide environmental changes without affecting the behavior of code outside of the function.
Examples include:

Changing the current directory with cd does not affect the parent shell. Running cd in a subshell is a cleaner alternative to pushd and popd.

Variable assignments are isolated to the subshell. You can temporarily change global settings like $PATH and $IFS without having to carefully save and restore their values before and after.

Shell options changed with set or shopt will be automatically restored when the subshell exits. I commonly write (set -x; some-commands) to temporarily enable command logging, for example.

Signal handlers installed with trap are only in effect in the subshell. You can install a custom INT (Ctrl-C) handler for the duration of a function, or a custom EXIT handler to run cleanup code when the function returns.
func() {(
    echo 'entering func' >&2
    trap 'echo exiting func >&2' EXIT

    ...
)}

If exit is called it won't cause the entire script to exit. This is useful if you want to call exit from several functions down the call stack as a sort of poor man's "exception".
Or if you want to source a script that might exit, wrapping it in a subshell will keep it from killing your script.
(
    . ./script-that-might-exit
    echo "script set \$foo to $foo"
    echo "script changed dir to $PWD"
)

Fun fact: Functions don't have to be delimited by curly braces. It's legal to omit the braces and use parentheses as the delimiters:
func() (
    # runs in a subshell
)

Comment: Even more generally, the body of a function just has to be a compound command. That means not just `{...}` and `(...)`, but `for` loops, `while` loops, `if` statements, etc.
Comment: @jhnc True. I extended the example to something that couldn't be done just `x`.
Comment: Setting *any* variable in the shell is potentially changing a global variable, unless the shell offers the `local` keyword - which modern shells do, but older shells don't, so you might see code which does it as a portability habit - because when you just do `foo=bar` in a function without a `local foo` in that function, you might be overwriting a variable named `foo` in the whole shell script, or even an environment variable named `foo`.
Answer: If exit is called in a (..) subshell, it will only terminate that expression. Moreover, the code is free to change the values of variables as well as global options (via set); those changes are not seen in the surrounding code and are gone when the expression exits, which can simplify reasoning about the correctness of the code.
When you use (...) inside a function, watch out of this pitfall: a return command inside the (...) won't return from the function; it will just terminate the (...), just like exit. If you have commands after the (...), they will then execute.


prove the next inequality $a_n=\frac {\ln (n+1)(2n-1)}{\ln (n)(2n+1)}<1$

Question: How to show that the element is less than one? From a certain $n$ onwards.
$$a_n=\frac {(2n-1)\ln (n+1)}{(2n+1)\ln (n)}<1$$
Thanks for the help!
Answer: The inequality is equivalent to $$(2n-1)\ln(n+1)<(2n+1)\ln(n)$$
equivalent to $$(n+1)^{2n-1}<n^{2n+1}$$
equivalent to $$\left[\left(1+\frac{1}{n}\right)^{n}\right]^{\frac{2n-1}{n}}<n^2$$
But $\left(1+\frac{1}{n}\right)^{n}<e$. Therefore $$\left[\left(1+\frac{1}{n}\right)^{n}\right]^{\frac{2n-1}{n}}<e^{\frac{2n-1}{n}}=e^{2-\frac{1}{n}}<e^{2}<n^2$$ for $n$ large ($n\geq3$ should be large enough).
Comment: It is true that for $n=2$ it is also true, but be careful on how you prove it. You have there inequalities pointing in the opposite directions.
Comment: Perhaps you wanted to write that, but in the first inequality you wrote $>$ while in the second you wrote $<$. Independently of them being correct or not, that is what I was saying you should avoid.
Comment: $\frac {\ln (n+1)(2n-1)}{\ln (n)(2n+1)} > \frac {\ln (n+1)(2n-1)}{\ln (n+1)(2n+1)}$  $\frac {\ln (n+1)(2n-1)}{\ln (n+1)(2n+1)} = \frac {(2n-1)}{(2n+1)}$ $\frac {(2n-1)}{(2n+1)} < 1$ for $n>1$ will that work as well?
Comment: I have shown that a greater element than the original is less than 1, isn't it adequate proof?
Answer: We just need to prove that $\frac{2n+1}{\log(n+1)}$ is an increasing function. But if:
$$ f(x) = \frac{2x+1}{\log(x+1)} $$
we have:
$$ f'(x) = \frac{2}{\log(x+1)}-\frac{(2x+1)}{(x+1)\log^2(x+1)}=\frac{2(x+1)\log(x+1)-(2x+1)}{(x+1)\log^2(x+1)} $$
and $f'(x)>0$ is equivalent to:
$$\log(x+1)>\frac{2x+1}{2x+2} $$
that holds for sure as soon as $x\geq 2 >e-1.$


How to multiply using big $O$ notation

Question: Suppose I have the following expression:
$$f(n) = \frac{\bigg(1+O(1/n)\bigg)}{\bigg(1+O(1/n)\bigg)\bigg(1+O(1/n)\bigg)}.$$
Is there a way to manipulate $f$ (i.e. multiply by terms involving $n$) in order to get an expression that looks like $(1+O(1))$?
Answer: If $a_n,b_n,c_n\in O(1/n)$, then
\begin{align}
\frac{1+a_n}{(1+b_n)(1+c_n)}-1
&=\frac{1+a_n-(1+b_n)(1+c_n)}{(1+b_n)(1+c_n)}\\
&=\frac{a_n-b_n-c_n-b_nc_n}{(1+b_n)(1+c_n)}\\
&\leq\frac{|a_n|+|b_n|+|c_n|+|b_n||c_n|}{|1+b_n||1+c_n|}\\
&=O(1/n)
\end{align}
hence
$$f(n)=\frac{1+O(1/n)}{(1+O(1/n))(1+O(1/n))}=1+O(1/n)$$
consequently, $f(n)=1+O(1)$ as well.

How to multiply using big $O$ notation


Porque está dando erro ao executar o código via Node?

Question: Estou tendo problemas em executar um código específico através do VScode.
Tenho o Node v4.2.6 instalado e o NPM v3.5.2 e quando vou rodar via Node o código:
let pais = 'Brasil'
console.log(qualquer)
Dá uma mensagem de erro no log:
(function (exports, require, module, __filename, __dirname) { let qualquer = 'Legal'
                                                              ^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
    at node.js:966:3
Answer: Olá,
Esta versão do Node pode não aceitar o "let", tente usar o var para testar. Algo assim:
<code>module.exports = funciona () {
   ...
   var pais = 'Brasil'
   console.log(qualquer)
}
</code>
Answer: Creio que seu Node não aceita let pois ele está numa versão antiga, tente atualizar a versão do node e teste seu código novamente.


Homotopy equivalence of total space of fibrations

Question: I am reading Selick's book "Introduction to homotopy theory", and he says in Cor 7.1.5 that for a fibration $p: E \rightarrow B$ with fiber $F$ and $B$ is CW complex, it is equivalent that there exists homotopy retraction $r: E \rightarrow F$ s.t. $rj \simeq 1_F$ where $j:F \rightarrow E$ is inclusion map,  and there exists $r: E \rightarrow F$ s.t. $(r, p): E \rightarrow F \times B$ is homotopy equivalence.
Just before stating this corollary he states that for fibrations $p:X\rightarrow B$ and $q : Y \rightarrow B$ with $B$ : CW-complex and for a map $f: X \rightarrow Y$,  $f$ is homotopy equivalence if and only if $f$ restricts homotopy equivalence on each fiber of $p $ and $q$.
I understand that $(r, p)$ is homotopy equivalence, then there exists homotopy retraction $r'$. But I have no idea why this converse holds. Does anybody prove that?
Thank you.
Answer: The corollary can only be true if we require the base space $B$ path connected, otherwise there may exist fibers of different homotopy type.
Let $F = p^{-1}(b)$ be the fiber over some $b$. We claim that the following are equivalent:
$(1)$ There exists a homotopy retraction $r : E \to F$.
$(2)$ There exists a map $r : E \to F$ such that $(r,p) : E \to F \times B$ is a fiber homotopy equivalence.
For each map $r : E \to F$ and each $x \in B$ define $r_x = r \mid_{F_x}: F_x = p^{-1}(x) \to F$. What we know is
(a) $r$ is a homotopy retraction if and only if $r_b = r \circ j \simeq 1_F$.
(b) $(r,p)$ is a fiber homotopy equivalence if and only if all $r_x$ are homotopy equivalences.
$(2) \Rightarrow (1)$: By (b) $r_b$ is a homotopy equivalence. Let $\sigma$ be a homotopy inverse for $r_b$. Define $r' = \sigma \circ r$. Then $r'_b = \sigma \circ r_b \simeq 1_F$. i.e. $r'$ is a homotopy retraction.
$(1) \Rightarrow (2)$: By (b) it suffices to show that all $r_x$ are homotopy equivalences. By (a) we know that $r_b$ is one. Let $u : I \to B$ be a path such that $u(0) = b, u(1) = x$. Form the pullback of $(u,p)$. This yields a fibration $p' : E' \to I$ and a map $u' : E' \to E$ such that $u' \circ p = p' \circ u$. The fibers $F'_t = (p')^{-1}(t)$ are mapped by $u'$ homeomorphically onto $F_{u(t)}$. Since $I$ is contractible, there exists a fiber homotopy equivalence $f : F'_0 \times I \to E'$. Define $i_t : F'_0 \to F'_0 \times I, i_t(y) = (y,t)$ and $i'_t : F'_0 \to F'_0 \times \{ t \}, i'_t(y) = (y,t)$. It suffices to show that $F'_0 \stackrel{i'_1}{\rightarrow} F'_0 \times \{ 1 \} \stackrel{f_1}{\rightarrow} F'_1 \stackrel{u'_1}{\rightarrow} F_x \stackrel{r_x}{\rightarrow} F$ is a homotopy equivalence. We have $r_x \circ u'_1 \circ f_1 \circ i'_1 = r \circ u' \circ f \circ i_1 \simeq r \circ u' \circ f \circ i_0 = r_b \circ u'_0 \circ f_0 \circ i'_0$, the latter being a homotopy equivalence.
Comment: Now I understand completely. Thank you!
Comment: thank you for your polite answer. Can we give an example that (1) → (2) does not hold when the base space B is not path-connected?
Comment: Take $B = \{ 0, 1 \}$, $E=  \{ 0 \} \times  \{ 0, 1 \}  \cup  \{ 1 \} \times  \{ 0 \}$ and $p : E \to B, p((a,b)) = a$. Let $F =  \{ 0 \} \times  \{ 0, 1 \}$ be the fiber over $0$. Then $r : E \to F,r((a,b)) = b$ is a retraction, but $E$ and $B \times F$ are not homotopy equivalent.


Creating a page to display several content types

Question: I have a website to build that requires several quite long pages, each containing several sections with quite different content in each.
A content type for each page isn't going to work as some sections would need to contain several entries, to be displayed as a slider.
So, I think, I need to create each section as a different content type and my problem becomes displaying several of these on the same page?
How can I display several content types on the same page?
Or maybe I could create a category for each page and add the types to those?
Answer: The Views module might help you for this purpose. It is highly configurable, and it allows you to display several items on a single page. Other modules can add functionality, like the Views Slideshow module.
Learning how to use Views takes some time, but there are many Views tutorials available in the web. A possible starting point might be this one:

https://dev.acquia.com/blog/a-totally-beginner-tutorial-to-views-for-drupal-7/22/01/2016/9576
Answer: You can use Panels  and Views to make it. With Panels you can create a new page with the regions you need, and with Views you can create blocks which can list nodes.
So you create Views of each content type you need and put it with Panels.
Answer: Views module is what you need.
Edit:
With this module, you can customize the presentation of the content (and different content types) of your Drupal site in many different ways on a single page and in many other ways. The fact that the module is so flexible, it can be assumed as "advanced web based query builder" and hence also makes it that confusing and complex at the same time.
It can take some time to grasp the whole concept of views as the other answer illustrates, however you can start using it to fit your simple needs in no time. You can follow some tutorials and make use of it in no time. The link that the other answer has provided looks like a good place to start. Here's one more.
Comment: Although answer points on the right direction it would be nice to give some explanation about what is Views and why it shouldbe used.
Comment: @tunic Agreed, and updated.


Feeds CSV import error

Question: I am trying to import a CSV from a url using the feeds module, I set everything up but when I start the import I get the following error:
An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: XXXXX StatusText: OK ResponseText: Fatal error: Unsupported operand types in XXXXX/sites/all/modules/feeds/includes/FeedsConfigurable.inc on line 149
I have traced that error to this code, specifically the last line:
<code>  public function getConfig() {
    $defaults = $this->configDefaults();
    return $this->config + $defaults;
  }
</code>
Any idea why I am getting this error?
I removed the default mappings because they were for an RSS feed, with GUID title etc....Could this have something to do with it? Are those required?
Answer: Are you running the alpha-4 or the current dev version of the feeds module? There have been a number of tickets discussing the same error condition you are having with node_save on import that coalesced around this issue on Drupal.org with a resulting patch committed to the current dev branch. You can try downloading the current dev branch of feeds (almost a year older now than alpha-4) or applying the patch at #25 in the above issue if you don't want to accept all of the updates since the last alpha release.
Comment: thanks yea had found it was going to answer my own question if nobody else got to it. Thanks again

Feeds CSV import error


Capture image and submit to other function in kivy python

Question: How to call other function on button click and return an image from that function?
Comment: Post a specific example with a specific question about the part you can't work out. If you can't work out more than one part, narrow the question down to a single component. For instance, if you don't know how to run a function when a button is pressed, ask about that.
Answer: See OpenCV UI documentation. Here is an example to store left clicked locations on an image:
<code>locs = []
global locs

def clicked_point(event, x, y, flag, image):
    if event == cv2.EVENT_LBUTTONDOWN:
        coords.append((x, y))

cv2.namedWindow('window-name')
cv2.imshow('window-name', img)

cv2.setMouseCallback('window-name', clicked_point, img)

cv2.waitKey(0)
cv2.destroyAllWindows()
</code>
You could define another function that does what you want.
Comment: I need in kivy, when a button is pressed it should display another image from other function, kindly provide code as im new to this


Find free port to Open Pseudo Terminal Using BSD

Question: I want to create a Pseudo Terminal using the BSD API. My understanding is that the Unix 98 API will automatically find a free port with posix_openpt() but with BSD API I need to check/find a free port to connect to. Is this correct?
So I need to do something like this...?
int fd, index = 0;
char serial_port[11]; // = "/dev/ptyp0"

while (true) {
    sprintf(serial_port, "/dev/ptyp%d", index);
    fd = open(serial_port, O_RDWR | O_NOCTTY | O_NONBLOCK);

    if (fd > 0)
        break;

    index++;
}

Do you know of a Pseudo Terminal tutorial that uses BSD? There are many using Unix 98 API but not many if any BSD API ones.
Comment: Is [`openpty()`](http://man7.org/linux/man-pages/man3/openpty.3.html) available? (Yes, I know this is a Linux manual page, but the function comes from BSD.)
Answer: Using the actual device path is problematic because the pathnames can differ from system to system.  Most portable applications which use pseudo terminals long ago were changed to use one of the library functions written to abstract away from the device path.
There is more than one flavor of BSD to consider, e.g., FreeBSD, NetBSD, OpenBSD (and others of course, including OSX).  xterm's configure script identifies features from each.  All have openpty in the "util" library, providing its prototype in util.h
POSIX provides posix_openpt which combines the functionality of openpty with others to provide a simpler interface.  However (though a documented interface), it is known to not work properly with OSX (see for example OSX 10.7.5 UTF-8 encoding over ssh as well as xterm's changelog).
Here are manpage links:

FreeBSD
NetBSD
OpenBSD
OSX